Skip to content

Commit

Permalink
logger library move to C
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 27, 2024
1 parent fb4c1d2 commit 14c4088
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 120 deletions.
7 changes: 0 additions & 7 deletions src/ccode.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ public extern int get_uid_by_user (string username);
//DOC: chech root user
public extern bool is_root ();

// ### nostd.c
// All private functions. please look util/interface.vala
private extern void no_stdin ();
private extern void no_stdout ();
private extern void no_stderr ();
private extern void reset_std ();

//DOC: ### sandbox.c
//DOC: `void sandbox (string type, string[] args):`
//DOC: run command in sandboxed area
Expand Down
1 change: 0 additions & 1 deletion src/ccode/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ void error(int status){
size_t len = 0;
char** errs = array_get(error_arr, &len);
if(len > 0 && !get_bool ("ignore-error")) {
printf("%ld\n", len);
for(i=0;i<len;i++){
if(errs[i] != NULL){
print_stderr(build_string("%s: %s", colorize(_ ("ERROR"), red), strdup(errs[i])));
Expand Down
61 changes: 52 additions & 9 deletions src/ccode/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>

/* functions from vala source */
void print(char* msg);
void print_stderr(char* msg);
void error_add(char* msg);
int get_bool(char* val);
void colorize_init();

/* string add function*/
char* str_add(char* s1, char* s2);

/* string builder */
char* build_string(char* format, ...);

/* headers of functions */

extern char* colorize(char* message, int color);
extern char* gettext(char* message);

#define yellow 33
#define green 32
#define _(A) gettext(A)

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

/* define function headers */
void cprint(char* msg);
void cprint_stderr(char* msg);
void cprint_dummy(char* msg);
void warning_fn(char* msg);
#ifdef DEBUG
void debug_fn(char* msg);
#endif
void info_fn(char* msg);


/* function pointer type definitions */
Expand All @@ -45,10 +52,46 @@ void print_fn(char* message, int new_line, int err){
write(err+1, message, strlen(message));
}

void warning_fn (char* message) {
print_stderr (build_string("%s: %s",colorize (_ ("WARNING"), yellow), message));
}

void info_fn (char* message) {
print_stderr (build_string("%s: %s",colorize (_ ("INFO"), yellow), message));
}

void cprint(char* message){
message = str_add(message, "\n");
write(1, message, strlen(message));
}


void set_terminal_title (char* msg) {
#if NOCOLOR
return;
#else
if (!get_bool ("no-color")) {
print_fn (build_string("%s%s%s","\x1b]2;", msg, "\x07"), false, false);
}
#endif
}


#ifdef DEBUG
static long last_debug_time = -1;
void debug_fn(char *message) {
if (last_debug_time == -1) {
last_debug_time = time(NULL);
}

time_t current_time = time(NULL);
int elapsed_time = (int)(current_time - last_debug_time);

print_stderr (build_string( "[%s:%d]: %s\n", "DEBUG", elapsed_time, message));

last_debug_time = current_time;
}
#endif
void cprint_stderr(char* message){
message = str_add(message, "\n");
write(2, message, strlen(message));
Expand Down
18 changes: 18 additions & 0 deletions src/include/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef LOGGER_H
#define LOGGER_H

void print(char* msg);
void warning(char* msg);
#if DEBUG
void debug(char* msg);
void debug_fn(char* message);
#endif
void info(char* msg);
void print_stderr(char* msg);
void warning_fn(char* message);
void info_fn(char* message);
void logger_init();
void set_terminal_title(char* message);

#endif /* LOGGER_H */

8 changes: 6 additions & 2 deletions src/interpreter.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public void add_script(string data) {
}
}

private extern void no_stdin();
private extern void reset_std();


public int ymp_run() {
variable_integer[] labels = {};
bool label_found = false;
Expand Down Expand Up @@ -78,10 +82,10 @@ public int ymp_run() {
}
lock_operation();
if (get_bool("disable-stdin")) {
nostdin();
no_stdin();
}
int status = operation_main(proc[i].type, proc[i].args);
resetstd();
reset_std();
unlock_operation();
if (status != 0) {
string type = proc[i].type;
Expand Down
38 changes: 0 additions & 38 deletions src/util/interface.vala
Original file line number Diff line number Diff line change
Expand Up @@ -104,41 +104,3 @@ public void print_with_amogus (string data) {
}

}

//DOC: `void nostdin ():`
//DOC: close stdin. Ignore input. This function may broke application.
public void nostdin () {
nostdin_enabled = true;
set_value_readonly ("ask", "false");
no_stdin ();
}

//DOC: `void nostdout ():`
//DOC: close stdout
public void nostdout () {
set_value_readonly ("no-stdout", "true");
no_stdout ();
}

//DOC: `void nostderr ():`
//DOC: close stderr
public void nostderr () {
set_value_readonly ("no-stderr", "true");
no_stderr ();
}

//DOC: `void resetstd ():`
//DOC: restore stdout and stdin
public void resetstd () {
set_value_readonly ("no-stdout", "false");
set_value_readonly ("no-stderr", "false");
reset_std ();
}

//DOC: `void nostd ():`
//DOC: close stdin, stdout and stderr
public void nostd () {
nostdin ();
nostdout ();
nostderr ();
}
63 changes: 0 additions & 63 deletions src/util/logger.vala

This file was deleted.

31 changes: 31 additions & 0 deletions src/vapi/logger.vapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[CCode (cheader_filename = "logger.h")]
public void print(string msg);

[CCode (cheader_filename = "logger.h")]
public void warning(string msg);

#if DEBUG
[CCode (cheader_filename = "logger.h")]
public void debug(string msg);

[CCode (cheader_filename = "logger.h")]
public void debug_fn (string message);
#endif

[CCode (cheader_filename = "logger.h")]
public void info(string msg);

[CCode (cheader_filename = "logger.h")]
public void print_stderr(string msg);

[CCode (cheader_filename = "logger.h")]
public void warning_fn (string message);

[CCode (cheader_filename = "logger.h")]
public void info_fn (string message);

[CCode (cheader_filename = "logger.h")]
public void logger_init ();

[CCode (cheader_filename = "logger.h")]
public void set_terminal_title (string message);

0 comments on commit 14c4088

Please sign in to comment.