-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlogging.h
67 lines (57 loc) · 3 KB
/
logging.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
#pragma once
#include <syscall.h>
#include <unistd.h>
#include <cassert>
#include <chrono>
#include "config.h"
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
namespace madfs {
struct FatalException : public std::exception {};
} // namespace madfs
/*
* The following macros used for assertion and logging
* Defined as macros since we want to have access to __FILE__ and __LINE__
*/
#define MADFS_FPRINTF(file, fmt, ...) \
do { \
auto now = std::chrono::high_resolution_clock::now(); \
std::chrono::duration<double> sec = now.time_since_epoch(); \
const char *s = strrchr(__FILE__, '/'); \
const char *caller_filename = s ? s + 1 : __FILE__; \
fprintf(file, "[Thread %d] %f [%14s:%-3d] " fmt "\n", ::madfs::tid, \
sec.count(), caller_filename, __LINE__, ##__VA_ARGS__); \
} while (0)
// PANIC_IF is active for both debug and release modes
#define PANIC_IF(expr, msg, ...) \
do { \
if (likely(!(expr))) break; \
MADFS_FPRINTF(stderr, "[PANIC] " msg ": %m", ##__VA_ARGS__); \
assert(false); \
throw ::madfs::FatalException(); \
} while (0)
#define PANIC(msg, ...) PANIC_IF(true, msg, ##__VA_ARGS__)
// LOG_* are not active in release mode
#define MADFS_LOG(level, msg, ...) \
do { \
if constexpr (!::madfs::BuildOptions::debug) break; \
if (level < ::madfs::runtime_options.log_level) break; \
constexpr const char *level_str_arr[] = { \
"[\u001b[37mTRACE\u001b[0m]", \
"[\u001b[32mDEBUG\u001b[0m]", \
"[\u001b[34mINFO\u001b[0m] ", \
"[\u001b[31mWARN\u001b[0m] ", \
}; \
constexpr const char *level_str = level_str_arr[level]; \
if (::madfs::log_file == nullptr) ::madfs::log_file = stderr; \
MADFS_FPRINTF(::madfs::log_file, "%s " msg, level_str, ##__VA_ARGS__); \
} while (0)
#define LOG_TRACE(msg, ...) MADFS_LOG(0, msg, ##__VA_ARGS__)
#define LOG_DEBUG(msg, ...) MADFS_LOG(1, msg, ##__VA_ARGS__)
#define LOG_INFO(msg, ...) MADFS_LOG(2, msg, ##__VA_ARGS__)
#define LOG_WARN(msg, ...) MADFS_LOG(3, msg, ##__VA_ARGS__)
namespace madfs {
inline __attribute__((tls_model("initial-exec"))) thread_local const pid_t tid =
static_cast<pid_t>(syscall(SYS_gettid));
inline FILE *log_file = stderr;
} // namespace madfs