-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlog.h
74 lines (51 loc) · 1.8 KB
/
log.h
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
#pragma once
#ifndef _LOG
#define _LOG
#include "pch.h"
#define HEADER "SFAE"
#define LOG_FILE "sfae.log"
namespace logging
{
static inline FILE* logfile;
void print(bool newLine, const char* tag, const std::string fmt, ...)
{
auto _fmt = fmt.c_str();
if (!logfile)
logfile = fopen(LOG_FILE, "w+");
auto time_since_epoch = std::time(nullptr);
auto local_time = std::localtime(&time_since_epoch);
printf("[%s] [%s] %02d:%02d:%02d ", HEADER, tag, local_time->tm_hour, local_time->tm_min, local_time->tm_sec);
fprintf(logfile, "[%s] [%s] %02d:%02d:%02d ", HEADER, tag, local_time->tm_hour, local_time->tm_min, local_time->tm_sec);
va_list args;
va_start(args, _fmt);
vprintf(_fmt, args);
vfprintf(logfile, _fmt, args);
va_end(args);
if (newLine)
{
printf("\n");
fprintf(logfile, "\n");
}
fflush(logfile);
}
void print_raw(const std::string fmt, ...)
{
auto _fmt = fmt.c_str();
if (!logfile)
logfile = fopen(LOG_FILE, "w+");
va_list args;
va_start(args, _fmt);
vprintf(_fmt, args);
vfprintf(logfile, _fmt, args);
va_end(args);
fflush(logfile);
}
}
#define log(fmt, ...) logging::print_raw(fmt, __VA_ARGS__)
#define info(fmt, ...) logging::print(true, "INFO", fmt, __VA_ARGS__)
#define info_no_newline(fmt, ...) logging::print(false, "INFO", fmt, __VA_ARGS__)
#define warn(fmt, ...) logging::print(true, "WARN", fmt, __VA_ARGS__)
#define warn_no_newline(fmt, ...) logging::print(false, "WARN", fmt, __VA_ARGS__)
#define err(fmt, ...) logging::print(true, " ERR", fmt, __VA_ARGS__)
#define err_no_newline(fmt, ...) logging::print(false, "ERR", fmt, __VA_ARGS__)
#endif