Changed logging system a bit...

This commit is contained in:
Ognjen Milan Robovic 2023-10-01 06:06:40 -04:00
parent 4f7de650a5
commit 9cd81639c0
2 changed files with 101 additions and 67 deletions

View File

@ -9,17 +9,20 @@
#ifndef XTANDARD_SOURCE #ifndef XTANDARD_SOURCE
#define XTANDARD_SOURCE #define XTANDARD_SOURCE
#include "xtandard.h" #include <xolatile/xtandard.h>
char * program_name = NULL; char * program_name = NULL;
char * program_mode = NULL; char * program_mode = NULL;
char * program_license = NULL; char * program_license = NULL;
char * log_notify = NULL;
int argument_count = 0; int argument_count = 0;
char * * argument_nick = NULL; char * * argument_nick = NULL;
char * * argument_name = NULL; char * * argument_name = NULL;
char * argument_input = NULL; char * argument_input = NULL;
char * argument_output = NULL; char * argument_output = NULL;
void (* * argument_function) (void) = NULL; void (* * argument_function) (void) = NULL;
int file_list_active = 0; int file_list_active = 0;
@ -43,7 +46,42 @@ void out (void * data, int size) {
(void) write (STDOUT_FILENO, data, (unsigned long int) size); (void) write (STDOUT_FILENO, data, (unsigned long int) size);
} }
void log_in (int type, int flag, char * data) {
char * type_mark [LOG_COUNT] = {
"[\033[0;32mSuccess\033[0m] ",
"[\033[0;33mWarning\033[0m] ",
"[\033[0;31mFailure\033[0m] ",
"[\033[0;34mComment\033[0m] "
};
if ((flag == 0) || (type <= -1) || (type >= LOG_COUNT)) {
return;
}
if (log_notify == NULL) {
log_notify = allocate (string_length (type_mark [type]) + string_length (data) + 2);
} else {
log_notify = reallocate (log_notify, string_length (log_notify) + string_length (type_mark [type]) + string_length (data) + 2);
log_notify [string_length (log_notify)] = '\0';
}
string_concatenate (log_notify, type_mark [type]);
string_concatenate (log_notify, data);
string_concatenate (log_notify, "\n");
}
void log_out (void) {
echo (log_notify);
log_notify = deallocate (log_notify);
}
void echo (char * data) { void echo (char * data) {
if (data == NULL) {
return;
}
out (data, string_length (data)); out (data, string_length (data));
} }
@ -59,7 +97,7 @@ void echo_byte (int byte) {
void fatal_failure (int condition, char * message) { void fatal_failure (int condition, char * message) {
if (condition != 0) { if (condition != 0) {
echo ("\033[1;31m[!]\033[0m "); echo ("\033[1;31m[FATAL_FAILURE]\033[0m ");
echo (message); echo (message);
echo ("\n"); echo ("\n");
@ -80,7 +118,9 @@ void limit (int * value, int minimum, int maximum) {
void * allocate (int size) { void * allocate (int size) {
char * data = NULL; char * data = NULL;
fatal_failure (size == 0, "allocate: Failed to allocate memory, size is zero."); if (size <= 0) {
return (NULL);
}
data = calloc ((unsigned long int) size, sizeof (* data)); data = calloc ((unsigned long int) size, sizeof (* data));
@ -90,7 +130,9 @@ void * allocate (int size) {
} }
void * reallocate (void * data, int size) { void * reallocate (void * data, int size) {
fatal_failure (size == 0, "reallocate: Failed to reallocate memory, size is zero."); if (size <= 0) {
return (data);
}
data = realloc (data, (unsigned long int) size); data = realloc (data, (unsigned long int) size);
@ -102,9 +144,9 @@ void * reallocate (void * data, int size) {
} }
void * deallocate (void * data) { void * deallocate (void * data) {
fatal_failure (data == NULL, "deallocate: Failed to deallocate memory, data is null pointer."); if (data != NULL) {
free (data); free (data);
}
return (NULL); return (NULL);
} }
@ -467,7 +509,9 @@ int character_compare_array (char character, char * character_array, int count)
int string_length (char * string) { int string_length (char * string) {
int length = 0; int length = 0;
fatal_failure (string == NULL, "string_length: String is null pointer."); if (string == NULL) {
return (0);
}
for (length = 0; string [length] != '\0'; ++length); for (length = 0; string [length] != '\0'; ++length);
@ -490,8 +534,9 @@ void string_reverse (char * string) {
void string_delete (char * string, int length) { void string_delete (char * string, int length) {
int i; int i;
fatal_failure (string == NULL, "string_delete: String is null pointer."); if ((string == NULL) || (length <= 0)) {
fatal_failure (length == 0, "string_delete: String length is zero."); return;
}
for (i = 0; i != length; ++i) { for (i = 0; i != length; ++i) {
string [i] = '\0'; string [i] = '\0';
@ -515,32 +560,28 @@ int string_compare (char * string_0, char * string_1) {
void string_copy (char * string_0, char * string_1) { void string_copy (char * string_0, char * string_1) {
int i = 0; int i = 0;
int length = 0;
fatal_failure (string_0 == NULL, "string_copy: Destination string is null pointer."); fatal_failure (string_0 == NULL, "string_copy: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_copy: Source string is null pointer."); fatal_failure (string_1 == NULL, "string_copy: Source string is null pointer.");
length = string_length (string_1); for (i = 0; i != string_length (string_1) + 1; ++i) {
for (i = 0; i != length; ++i) {
string_0 [i] = string_1 [i]; string_0 [i] = string_1 [i];
} }
} }
void string_concatenate (char * string_0, char * string_1) { void string_concatenate (char * string_0, char * string_1) {
int i = 0;
int length_0 = 0;
int length_1 = 0;
fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null pointer."); fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_concatenate: Source string is null pointer."); fatal_failure (string_1 == NULL, "string_concatenate: Source string is null pointer.");
length_0 = string_length (string_0); string_0 += string_length (string_0);
length_1 = string_length (string_1);
for (i = 0; i != length_1; ++i) { while (* string_1 != '\0') {
string_0 [length_0 + i] = string_1 [i]; * string_0++ = * string_1++;
/*++string_0;
++string_1;*/
} }
* string_0 = '\0';
} }
int string_compare_limit (char * string_0, char * string_1, int limit) { int string_compare_limit (char * string_0, char * string_1, int limit) {
@ -635,7 +676,7 @@ int memory_compare (void * memory, void * source, int length) {
fatal_failure (source == NULL, "memory_compare: Source is null pointer."); fatal_failure (source == NULL, "memory_compare: Source is null pointer.");
if (length <= 0) { if (length <= 0) {
return; return (-1);
} }
for (i = 0; (cast_0 [i] != '\0') && (cast_1 [i] != '\0'); ++i) { for (i = 0; (cast_0 [i] != '\0') && (cast_1 [i] != '\0'); ++i) {
@ -702,7 +743,12 @@ int encode_byte (char * byte) {
} }
char * decode_byte (int byte) { char * decode_byte (int byte) {
static char * decode = " "; char * decode = " ";
fatal_failure ((byte <= -1) || (byte >= 256), "decode_byte: Byte is out of range.");
decode [0] = byte / 16;
decode [1] = byte % 16;
return (decode); return (decode);
} }

View File

@ -14,54 +14,39 @@
#include <fcntl.h> #include <fcntl.h>
enum { enum {
EFFECT_NORMAL, LOG_SUCCESS, LOG_WARNING, LOG_FAILURE, LOG_COMMENT,
EFFECT_BOLD, LOG_COUNT
EFFECT_DARK, };
EFFECT_ITALIC,
EFFECT_UNDERLINE, enum {
EFFECT_BLINK, FILE_TYPE_TEXT, FILE_TYPE_COMMON_ASSEMBLY, FILE_TYPE_FLAT_ASSEMBLY, FILE_TYPE_GNU_ASSEMBLY,
EFFECT_UNDEFINED, FILE_TYPE_NETWIDE_ASSEMBLY, FILE_TYPE_YET_ANOTHER_ASSEMBLY, FILE_TYPE_C_SOURCE, FILE_TYPE_C_HEADER,
EFFECT_REVERSE, FILE_TYPE_ADA_BODY, FILE_TYPE_ADA_SPECIFICATION, FILE_TYPE_CPP_SOURCE, FILE_TYPE_CPP_HEADER,
FILE_TYPE_COUNT
};
enum {
EFFECT_NORMAL, EFFECT_BOLD, EFFECT_DARK, EFFECT_ITALIC, EFFECT_UNDERLINE, EFFECT_BLINK, EFFECT_UNDEFINED, EFFECT_REVERSE,
EFFECT_COUNT EFFECT_COUNT
}; };
enum { enum {
COLOUR_GREY, COLOUR_GREY, COLOUR_RED, COLOUR_GREEN, COLOUR_YELLOW, COLOUR_BLUE, COLOUR_PINK, COLOUR_CYAN, COLOUR_WHITE,
COLOUR_RED,
COLOUR_GREEN,
COLOUR_YELLOW,
COLOUR_BLUE,
COLOUR_PINK,
COLOUR_CYAN,
COLOUR_WHITE,
COLOUR_COUNT COLOUR_COUNT
}; };
enum {
FILE_TYPE_TEXT,
FILE_TYPE_COMMON_ASSEMBLY,
FILE_TYPE_FLAT_ASSEMBLY,
FILE_TYPE_GNU_ASSEMBLY,
FILE_TYPE_NETWIDE_ASSEMBLY,
FILE_TYPE_YET_ANOTHER_ASSEMBLY,
FILE_TYPE_C_SOURCE,
FILE_TYPE_C_HEADER,
FILE_TYPE_ADA_BODY,
FILE_TYPE_ADA_SPECIFICATION,
FILE_TYPE_CPP_SOURCE,
FILE_TYPE_CPP_HEADER,
FILE_TYPE_COUNT
};
extern char * program_name; extern char * program_name;
extern char * program_mode; extern char * program_mode;
extern char * program_license; extern char * program_license;
extern char * log_notify;
extern int argument_count; extern int argument_count;
extern char * * argument_nick; extern char * * argument_nick;
extern char * * argument_name; extern char * * argument_name;
extern char * argument_input; extern char * argument_input;
extern char * argument_output; extern char * argument_output;
extern void (* * argument_function) (void); extern void (* * argument_function) (void);
extern int file_list_active; extern int file_list_active;
@ -74,6 +59,9 @@ extern char * * file_list_data;
extern void in (void *, int); extern void in (void *, int);
extern void out (void *, int); extern void out (void *, int);
extern void log_in (int, int, char *);
extern void log_out (void);
extern void echo (char *); extern void echo (char *);
extern void echo_new_line (void); extern void echo_new_line (void);
extern void echo_byte (int); extern void echo_byte (int);