This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sd_errors.c
112 lines (107 loc) · 3.41 KB
/
sd_errors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2022 Valentin-Ioan VINTILA (313CA / 2021-2022).
// All rights reserved.
// Include the asscociated header file
#include "sd_errors.h"
// This function tests if an error is critical or not.
// - - - - -
// error_t e = A given error that should be checked.
// - - - - -
// Return: bool = true if the error was critical.
bool is_critical(error_t e)
{
// All critical errors are negative! It's as if this was done by choice...
return (e < NO_ERROR);
}
// This function provides a safe way to output information to a log file.
// - - - - -
// log_t type = The message will be prefixed with a string based on this option
// The next arguments are similar to a printfs'
void sd_log(log_t type, const char *format, ...)
{
static int log_counter;
static char log_path[32];
static bool first_time = true;
FILE *flog;
if (first_time) {
sprintf(log_path, "log%d.txt", log_counter);
flog = fopen(log_path, "wt+");
} else {
flog = fopen(log_path, "at+");
}
if (!flog) { // This is a huge problem!
fprintf(stderr, "[FATAL ERROR] - Cannot write to log file\n");
exit(EXIT_FAILURE);
}
if (first_time) {
fprintf(flog, "- - - Log %d started - - -\n", log_counter);
first_time = false;
}
if (type == LOG_CLOSE_SIGNAL) {
fprintf(flog, "\n\n- - - Log %d ended - - -\n", log_counter);
first_time = true;
++log_counter;
} else {
va_list argptr;
va_start(argptr, format);
if (type == LOG_DEBUG) {
#ifdef SD_LOG_DEBUG
fprintf(flog, "\n[d]: ");
vfprintf(flog, format, argptr);
#endif
} else if (type == LOG_INFO) {
fprintf(flog, "\n[i]: ");
vfprintf(flog, format, argptr);
} else if (type == LOG_WARN) {
fprintf(flog, "\n[warn]: ");
vfprintf(flog, format, argptr);
} else if (type == LOG_ERROR) {
#ifdef SD_LOG_FULL
fprintf(stderr, "\n[ERROR]: ");
vfprintf(stderr, format, argptr);
#endif
fprintf(flog, "\n[ERROR]: ");
vfprintf(flog, format, argptr);
} else if (type == LOG_GENERAL) {
vfprintf(flog, format, argptr);
}
va_end(argptr);
}
fclose(flog);
}
// This function is a faster way to output a default log message for an error.
// - - - - -
// error_t e = The error based on which a message shall be written.
void sd_log_error(error_t e)
{
if (e == CRITICAL_OUTPUT_FILE)
sd_log(LOG_ERROR, "Could not open one of the output files!");
else if (e == CRITICAL_INPUT_SP)
sd_log(LOG_ERROR, "Could not open input file 'spammers'!");
else if (e == CRITICAL_INPUT_KW)
sd_log(LOG_ERROR, "Could not open 'keywords' / one 'in_' file!");
else if (e == CRITICAL_INPUT_DIR)
sd_log(LOG_ERROR, "Could not open the input directory file!");
else if (e == CRITICAL_INPUT_FILE)
sd_log(LOG_ERROR, "Could not open the one of the emails!");
else if (e == CRITICAL_SAFE_REALLOC)
sd_log(LOG_ERROR, "safe_realloc() failed!");
else if (e == CRITICAL_SAFE_MALLOC)
sd_log(LOG_ERROR, "safe_malloc() failed!");
else if (e == CRITICAL_SAFE_CALLOC)
sd_log(LOG_ERROR, "safe_calloc() failed!");
else if (e == CRITICAL_UNKNOWN)
sd_log(LOG_ERROR, "Unknown critical error occured!");
else if (e == NO_ERROR)
sd_log(LOG_DEBUG, "No error occured.");
else if (e == WARNING_UNKNOWN)
sd_log(LOG_WARN, "Unknown warning occured.");
else if (e < 0)
sd_log(LOG_ERROR, "Even sd_log_error() failed! Error code: %d", e);
else
sd_log(LOG_WARN, "Even sd_log_error() failed! Warning code: %d", e);
}
// This function sends the closing signal to the log.
void sd_log_close(void)
{
sd_log(LOG_CLOSE_SIGNAL, "");
}