Fix const warning
[fw/stlink] / src / logging.c
1 /*
2  * UglyLogging.  Slow, yet another wheel reinvented, but enough to make the
3  * rest of our code pretty enough.
4  *
5  */
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <time.h>
11
12 #include "stlink/logging.h"
13
14 static int max_level;
15
16 int ugly_init(int maximum_threshold) {
17     max_level = maximum_threshold;
18     return 0;
19 }
20
21 int ugly_log(int level, const char *tag, const char *format, ...) {
22     if (level > max_level) {
23         return 0;
24     }
25     va_list args;
26     va_start(args, format);
27     time_t mytt = time(NULL);
28     struct tm *tt;
29     tt = localtime(&mytt);
30     fprintf(stderr, "%d-%02d-%02dT%02d:%02d:%02d ", tt->tm_year + 1900, tt->tm_mon + 1, tt->tm_mday, tt->tm_hour, tt->tm_min, tt->tm_sec);
31     switch (level) {
32     case UDEBUG:
33         fprintf(stderr, "DEBUG %s: ", tag);
34         break;
35     case UINFO:
36         fprintf(stderr, "INFO %s: ", tag);
37         break;
38     case UWARN:
39         fprintf(stderr, "WARN %s: ", tag);
40         break;
41     case UERROR:
42         fprintf(stderr, "ERROR %s: ", tag);
43         break;
44     case UFATAL:
45         fprintf(stderr, "FATAL %s: ", tag);
46         vfprintf(stderr, format, args);
47         exit(EXIT_FAILURE);
48         // NEVER GETS HERE!!!
49         break;
50     default:
51         fprintf(stderr, "%d %s: ", level, tag);
52         break;
53     }
54     vfprintf(stderr, format, args);
55     va_end(args);
56     return 1;
57 }