Skip to content

Commit

Permalink
ccode uses ymp logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 27, 2024
1 parent 14c4088 commit e5d9d7a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 46 deletions.
28 changes: 7 additions & 21 deletions src/ccode/archive-create.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 32,8 @@


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

#ifndef PATH_MAX
#define PATH_MAX 1024
Expand All @@ -50,16 52,6 @@ int get_bool(char*variable);
int isexists(const char*path);
#endif

#ifndef debug
void debug(char* msg);
#endif

#ifndef error_add
void error_add(char* msg);
int has_error();
void error(int num);
#endif

void archive_set_type(archive *data, char* form, char* filt){
#ifdef debug
debug("Archive type changed");
Expand Down Expand Up @@ -128,7 120,7 @@ void archive_write(archive *data, const char *outname, const char **filename) {
entry = NULL;
while (*filename) {
if(!isexists(*filename)){
fprintf(stderr,"Non-existent enty detected: %s\n",*filename);
fwarning("Non-existent enty detected: %s\n",*filename);
continue;
}
lstat(*filename, &st);
Expand Down Expand Up @@ -166,16 158,12 @@ void archive_write(archive *data, const char *outname, const char **filename) {
#endif
len = readlink(*filename,link,sizeof(link));
if(len < 0){
fprintf(stderr,"Broken symlink: %s\n",*filename);
ferror_add("Broken symlink: %s\n",*filename);
error_add("Failed to create archive");
break;
}
link[len] = '\0';
#ifdef DEBUG
if(get_bool("debug")){
fprintf(stderr,"Symlink: %s %s\n",*filename, link);
}
#endif
fdebug("Symlink: %s %s\n",*filename, link);
archive_entry_set_filetype(entry, AE_IFLNK);
archive_entry_set_symlink(entry, link);
} else if (S_ISREG(st.st_mode)) {
Expand All @@ -196,16 184,14 @@ void archive_write(archive *data, const char *outname, const char **filename) {
type = "unknown";
#endif
archive_entry_set_filetype(entry, AE_IFREG);
fprintf(stderr,"Unknown enty detected: %s (%d %d)\n",*filename,st.st_mode,AE_IFREG);
ferror_add("Unknown enty detected: %s (%d %d)\n",*filename,st.st_mode,AE_IFREG);
error_add("Failed to create archive");
}
if(has_error()){
error(2);
}
#ifdef DEBUG
if(get_bool("debug")){
fprintf(stderr,"Compress: %s type %s (%d)\n",*filename,type,st.st_mode);
}
fdebug("Compress: %s type %s (%d)\n",*filename,type,st.st_mode);
#endif
archive_entry_set_perm(entry, 0644);
archive_write_header(a, entry);
Expand Down
23 changes: 2 additions & 21 deletions src/ccode/archive-extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 14,7 @@ extern char* build_string(char* format, ...);
#endif


#if DEBUG
extern void debug(char* message);
#define fdebug(A, ...) \
do { \
char* message = build_string(A, ##__VA_ARGS__); \
debug(message); \
free(message); \
} while (0)
#else
#define debug(A)
#define fdebug(A,...)
#endif

extern void info(char* message);
#define finfo(A, ...) \
do { \
char* message = build_string(A, ##__VA_ARGS__); \
info(message); \
free(message); \
} while (0)
#include <logger.h>

#include <archive_extract.h>

Expand All @@ -55,7 36,7 @@ void archive_load(archive *data, char* path) {
archive_set_type(data, "zip", "none");
}

void archive_load_archive(archive *data) {
static void archive_load_archive(archive *data) {
data->archive = archive_read_new();
archive_read_support_filter_all(data->archive);
archive_read_support_format_all(data->archive);
Expand Down
5 changes: 4 additions & 1 deletion src/ccode/brainfuck.c
Original file line number Diff line number Diff line change
@@ -1,6 1,9 @@
#include <stdio.h>
#include <string.h>

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

#ifndef get_bool
int get_bool(char* msg);
#endif
Expand All @@ -15,7 18,7 @@ void brainfuck(char * code, unsigned int size) {
}
for (i = 0; i < strlen(code); i ) {
if(ptr >=size){
fprintf(stderr,"Failed to run brainfuck code: %s\n","Out of memory");
ferror_add("Failed to run brainfuck code: %s\n","Out of memory");
return;
}
if (code[i] == '>') {
Expand Down
6 changes: 5 additions & 1 deletion src/ccode/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,10 @@
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>


#include <logger.h>

void skeleton_daemon(){
pid_t pid;

Expand Down Expand Up @@ -39,6 43,6 @@ void skeleton_daemon(){
if (pid > 0)
exit(EXIT_SUCCESS);

fprintf (stderr, "Daemon started:\t[%d]\n",getpid () 1);
print_stderr(build_string("Daemon started:\t[%d]\n",getpid () 1));

}
2 changes: 1 addition & 1 deletion src/ccode/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 7,7 @@

#include <jobs.h>

void* worker_thread(void* arg) {
static void* worker_thread(void* arg) {
jobs* j = (jobs*)arg;
while (1) {
if (j->finished >= j->total) {
Expand Down
11 changes: 10 additions & 1 deletion src/include/error.h
Original file line number Diff line number Diff line change
@@ -1,4 1,13 @@
#include <stdbool.h>
#include <stdlib.h>
void error(int status);
void error_add(char* message);
bool has_error();
bool has_error();

#define ferror_add(A, ...) \
do { \
char* message = build_string(A, ##__VA_ARGS__); \
error_add(message); \
free(message); \
} while (0)

28 changes: 28 additions & 0 deletions src/include/logger.h
Original file line number Diff line number Diff line change
@@ -1,6 1,8 @@
#ifndef LOGGER_H
#define LOGGER_H

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

void print(char* msg);
void warning(char* msg);
#if DEBUG
Expand All @@ -14,5 16,31 @@ void info_fn(char* message);
void logger_init();
void set_terminal_title(char* message);

#if DEBUG
#define fdebug(A, ...) \
do { \
char* message = build_string(A, ##__VA_ARGS__); \
debug(message); \
free(message); \
} while (0)
#else
#define debug(A)
#define fdebug(A,...)
#endif

#define finfo(A, ...) \
do { \
char* message = build_string(A, ##__VA_ARGS__); \
info(message); \
free(message); \
} while (0)

#define fwarning(A, ...) \
do { \
char* message = build_string(A, ##__VA_ARGS__); \
warning(message); \
free(message); \
} while (0)

#endif /* LOGGER_H */

0 comments on commit e5d9d7a

Please sign in to comment.