Skip to content

Commit

Permalink
more ccode functions uses logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 27, 2024
1 parent e5d9d7a commit 54ff1c3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
17 changes: 10 additions & 7 deletions src/ccode/fetcher_curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ char* get_value(char* variable);
int get_bool(char* variable);
#endif

#include <logger.h>
#include <error.h>

#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
Expand All @@ -29,8 +32,8 @@ void init_string(struct string *s) {
s->len = 0;
s->ptr = calloc(1, s->len+1);
if (s->ptr == NULL) {
fprintf(stderr, "calloc() failed\n");
exit(EXIT_FAILURE);
ferror_add("calloc() failed\n");
error(1);
}
s->ptr[0] = '\0';
}
Expand All @@ -41,8 +44,8 @@ static size_t write_data_to_string(void *ptr, size_t size, size_t nmemb, void *s
size_t new_len = s->len + size*nmemb;
s->ptr = realloc(s->ptr, new_len+1);
if (s->ptr == NULL) {
fprintf(stderr, "realloc() failed\n");
exit(EXIT_FAILURE);
ferror_add( "realloc() failed\n");
error(1);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
Expand Down Expand Up @@ -94,15 +97,15 @@ int fetch(char* url, char* path){
FILE *fp;
strcpy(fetcher_filename,path);
CURL* curl=curl_easy_init();
printf("Downloading: %s\n",path);
finfo("Downloading: %s\n",path);
fp = fopen(path,"wb");
curl_options_common(curl, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data_to_file);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
CURLcode res;
res = curl_easy_perform(curl);
if(res != CURLE_OK || res == CURLE_HTTP_RETURNED_ERROR){
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
ferror_add("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
return 0;
}
Expand All @@ -123,7 +126,7 @@ char* fetch_string(char* url){
CURLcode res;
res = curl_easy_perform(curl);
if(res != CURLE_OK){
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
ferror_add("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return (char*)(s.ptr);
}
curl_easy_cleanup(curl);
Expand Down
18 changes: 11 additions & 7 deletions src/ccode/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void debug(char* msg);
#endif
char* str_add(char* s1, char* s2);

#include <logger.h>
#include <error.h>

uint64_t filesize(char* path){
struct stat st;
stat(path, &st);
Expand Down Expand Up @@ -91,8 +94,9 @@ int isdir(char *path){
}

#define c_mkdir(A, B) \
if (mkdir(A, B) < 0) { \
fprintf(stderr, "Error: %s %s\n", "failed to create directory.", A); \
if (isexists(A)) \
if (mkdir(A, B) < 0) { \
ferror_add("%s %s\n", "failed to create directory.", A); \
}

#ifndef isexists
Expand Down Expand Up @@ -171,9 +175,9 @@ char * readfile_raw(const char * filename) {

if (err) {
if (err == FILE_NOT_EXIST) {
fprintf(stderr, "Error: %s (%d) %s\n", "file not found.", err, filename);
ferror_add("%s (%d) %s\n", "file not found.", err, filename);
} else if (err == FILE_READ_ERROR) {
fprintf(stderr, "Error: %s (%d) %s\n", "failed to read file.", err,filename);
ferror_add("%s (%d) %s\n", "failed to read file.", err,filename);
}
return "";
} else {
Expand All @@ -200,7 +204,7 @@ void cd(char *path) {

/* Set the current directory to the specified path */
if (chdir(path) != 0) {
perror("Error changing directory");
ferror_add("Error changing directory: %s", path);
return;
}
}
Expand Down Expand Up @@ -273,13 +277,13 @@ char* safedir(char* dir) {
void write_to_file(const char *which, const char *format, ...) {
FILE * fu = fopen(which, "w");
if (fu == NULL) {
fprintf(stderr,"Error: Cannot open for write %s\n", which);
ferror_add("Cannot open for write %s\n", which);
return;
}
va_list args;
va_start(args, format);
if (vfprintf(fu, format, args) < 0) {
fprintf(stderr,"Error: Cannot write %s\n", which);
ferror_add("Cannot write %s\n", which);
}
fclose(fu);
}
Expand Down
5 changes: 4 additions & 1 deletion src/ccode/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
#define _plugin
#include <stdio.h>
#include <dlfcn.h>

#include <error.h>

void load_plugin(char* path){
void *handle;
handle = dlopen(path, RTLD_LAZY);
if (!handle) {
fprintf(stderr, "Failed to load plugin: %s from %s\n ",dlerror(), path);
ferror_add("Failed to load plugin: %s from %s\n ",dlerror(), path);
return;
}
dlerror();
Expand Down
9 changes: 6 additions & 3 deletions src/ccode/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ char* which(char* path);
char* str_add(char* str1, char* str2);
#endif


#include <error.h>

int locked=0;
void single_instance(){
if(locked){
Expand All @@ -32,7 +35,7 @@ void single_instance(){
locked = 1;
if(rc) {
if(EWOULDBLOCK == errno){
puts("Another ymp instance is already running");
error_add("Another ymp instance is already running");
exit(31);
}
}
Expand Down Expand Up @@ -70,7 +73,7 @@ int run_args(char **command) {
return WEXITSTATUS(status);
} else {
/* Child process did not terminate normally */
fprintf(stderr, "Child process did not terminate normally.\n");
error_add("Child process did not terminate normally.\n");
return EXIT_FAILURE;
}
}
Expand Down Expand Up @@ -113,7 +116,7 @@ int run_args_silent(char **command) {
return WEXITSTATUS(status);
} else {
/* Child process did not terminate normally */
fprintf(stderr, "Child process did not terminate normally.\n");
error_add( "Child process did not terminate normally.\n");
return EXIT_FAILURE;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ccode/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ char* url_decode(const char *input) {
char *output = (char *)calloc((cnt + 1), sizeof(char));

if (output == NULL) {
fprintf(stderr, "Memory allocation failed\n");
perror("Memory allocation failed\n");
return (char*) input;
}

Expand Down Expand Up @@ -173,7 +173,7 @@ char* url_encode(const char *input) {
char *output = (char *)malloc((cnt + 1) * sizeof(char));

if (output == NULL) {
fprintf(stderr, "Memory allocation failed\n");
perror( "Memory allocation failed\n");
return (char*) input;
}

Expand Down
3 changes: 3 additions & 0 deletions src/include/error.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <stdbool.h>
#include <stdlib.h>

extern char* build_string(char* message, ...);

void error(int status);
void error_add(char* message);
bool has_error();
Expand Down

0 comments on commit 54ff1c3

Please sign in to comment.