-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.h
75 lines (60 loc) · 2.64 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
68
69
70
71
72
73
74
75
#pragma once
#include <stdarg.h>
#include <stdexcept>
namespace ZipSync {
//how severe logged message is
enum Severity {
sevVerbose = 1,
sevDebug,
sevInfo,
sevWarning,
sevError, //throws exception
sevFatal, //terminates program immediately
};
//some messages are assigned nonzero "code"
//it allows intercepting them in error exceptions and in tests
enum LogCode {
lcGeneric = 0,
lcAssertFailed, //ZipSyncAssert has failed
lcCantOpenFile, //unexpected fail when opening file
lcMinizipError, //unexpected error from minizip function
lcUserInterrupt, //generated because progress callback asked to interrupt
lcDownloadTooSlow, //curl download stopped as too slow
//the remaining log codes are intercepted during testing
lcRenameZipWithoutRepack,
lcRepackZip,
};
//thrown when message with "error" severity is posted
class ErrorException : public std::runtime_error {
int _code;
public:
ErrorException(const char *message, int code = lcGeneric);
int code() const { return _code; }
};
//base class of Logger
class Logger {
public:
virtual ~Logger();
virtual void Message(LogCode code, Severity severity, const char *message) = 0;
void logf(Severity severity, LogCode code, const char *format, ...);
void logv(Severity severity, LogCode code, const char *format, va_list args);
void verbosef (LogCode code, const char *format, ...);
void debugf (LogCode code, const char *format, ...);
void infof (LogCode code, const char *format, ...);
void warningf (LogCode code, const char *format, ...);
[[noreturn]] void errorf (LogCode code, const char *format, ...);
[[noreturn]] void fatalf (LogCode code, const char *format, ...);
void verbosef (const char *format, ...);
void debugf (const char *format, ...);
void infof (const char *format, ...);
void warningf (const char *format, ...);
[[noreturn]] void errorf (const char *format, ...);
[[noreturn]] void fatalf (const char *format, ...);
};
//global instance of logger, used for everything
extern Logger *g_logger;
std::string formatMessage(const char *format, ...);
std::string assertFailedMessage(const char *code, const char *file, int line);
#define ZipSyncAssert(cond) if (!(cond)) g_logger->errorf(lcAssertFailed, assertFailedMessage(#cond, __FILE__, __LINE__).c_str()); else ((void)0)
#define ZipSyncAssertF(cond, ...) if (!(cond)) g_logger->errorf(lcAssertFailed, __VA_ARGS__); else ((void)0);
}