-
Notifications
You must be signed in to change notification settings - Fork 2
/
mambo_logger.c
116 lines (95 loc) · 2.52 KB
/
mambo_logger.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
113
114
115
116
#ifdef DEBUG
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include "mambo_logger.h"
#define LOG_NAME_LENGTH 60
#define LOG_MAX_LOGGERS 20
static mambo_logger_t global_loggers[LOG_MAX_LOGGERS] = {0};
static int log_nr = 0;
mambo_logger_t* _log_search(char *name) {
if (name == NULL)
name = "";
for (int i = 0; i < LOG_MAX_LOGGERS; i++) {
if (strcmp(global_loggers[i].name, name) == 0)
return &global_loggers[i];
}
return NULL;
}
void _log_create(char *name, int init_flags)
{
mambo_logger_t *logger = _log_search(NULL);
strncpy(logger->name, name, LOG_NAME_LENGTH);
logger->flags = init_flags;
}
void _log_open(char *name, char *modes)
{
mambo_logger_t *logger = _log_search(name);
FILE *logfp = _log_open_raw_fp(name, modes);
if (logfp != NULL) {
fprintf(logfp, "==== START NEW LOGGING ====\n");
logger->log_fp = logfp;
logger->flags |= LOG_OPEN;
} else
logger->flags &= ~LOG_OPEN;
}
void _log_close(char *name)
{
mambo_logger_t *logger = _log_search(name);
FILE *logfp = logger->log_fp;
if (logfp != NULL && logger->flags & LOG_OPEN) {
fclose(logfp);
logger->flags &= ~LOG_OPEN;
}
}
FILE* _log_open_raw_fp(char *name, char* modes)
{
char filename[LOG_NAME_LENGTH + 4];
snprintf(filename, LOG_NAME_LENGTH + 4, "%s.log", name);
FILE *fp;
fp = fopen(filename, modes);
return fp;
}
void _log_set_secondary_fp(char *name, FILE *fp)
{
mambo_logger_t *logger = _log_search(name);
if (logger == NULL) {
fprintf(stderr, "logger %s not avaluable!\n", name);
return;
}
logger->log_sec_fp = fp;
}
void _log(char *name, const char *format, ...)
{
mambo_logger_t *logger = _log_search(name);
if (logger == NULL) {
fprintf(stderr, "logger %s not avaluable!\n", name);
return;
}
va_list arglist;
if (logger->log_fp != NULL && logger->flags & LOG_OPEN) {
fprintf(logger->log_fp, "%-5d [ %20s ] ", log_nr, logger->name);
if (logger->log_sec_fp != NULL)
fprintf(logger->log_sec_fp, "%-5d [ %20s ] ", log_nr, logger->name);
if (logger->flags & LOG_STDOUT) {
fprintf(stdout, "%-5d [ %20s ] ", log_nr, logger->name);
}
if (logger->flags & LOG_STDERR) {
fprintf(stderr, "%-5d [ %20s ] ", log_nr, logger->name);
}
va_start(arglist, format);
vfprintf(logger->log_fp, format, arglist);
if (logger->log_sec_fp != NULL)
vfprintf(logger->log_sec_fp, format, arglist);
if (logger->flags & LOG_STDOUT) {
vfprintf(stdout, format, arglist);
}
if (logger->flags & LOG_STDERR) {
vfprintf(stderr, format, arglist);
}
va_end(arglist);
log_nr++;
}
}
#endif