-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.c
More file actions
executable file
·71 lines (55 loc) · 1.71 KB
/
log.c
File metadata and controls
executable file
·71 lines (55 loc) · 1.71 KB
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
// --------------------------------------------------------
// uweb : a minimal web server which compile under MacOS, Linux and Windows
// by Ph. Jounin November 2019
//
// License: GPLv2
//
// module: log.c
// ---------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include "compat.h"
#include "log.h"
// from uhttps.h
extern struct S_Settings
{
int uVerbose;
int timestamp;
} sSettings;
static struct tm *my_localtime_r(const time_t *tim, struct tm *result)
{
static int poor_man_mutex=1;
struct tm *t;
while (poor_man_mutex<1) ssleep(5);
--poor_man_mutex;
if ( (t = localtime(tim)) != NULL) *result = *t;
++poor_man_mutex;
return t ? result : NULL;
}
void LOG (int verbose_level, const char *fmt, ...)
{
char date[sizeof "2020-04-06 11:08:43, "];
char buff[1024];
va_list args;
time_t now = time(NULL);
struct tm tNow = { 0 } ; // ensure it is initialized for the compiler
if (sSettings.uVerbose < verbose_level) return ;
va_start (args, fmt);
vsnprintf (buff, sizeof buff, fmt, args);
buff[sizeof buff - 1] = 0;
va_end (args);
if (sSettings.timestamp)
{
now = time(NULL);
my_localtime_r (&now, &tNow);
// modulus are used to prove to smart compilers that the integers will not exceed format size
sprintf (date, "%04u-%02u-%02u %02u:%02u:%02u, ",
(tNow.tm_year + 1900) % 10000, (tNow.tm_mon + 1) % 100,
tNow.tm_mday % 100, tNow.tm_hour % 100,
tNow.tm_min % 100, tNow.tm_sec % 100);
fputs (date, verbose_level<=ERROR ? stderr : stdout);
}
fputs (buff, verbose_level<=ERROR ? stderr : stdout);
} // LOG