DebugServer2
Loading...
Searching...
No Matches
Log.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/Base.h"
14#include "DebugServer2/Utils/CompilerSupport.h"
15
16#include <cinttypes>
17#include <cstdarg>
18#include <cstdint>
19#include <cstdio>
20#include <cstdlib>
21
22namespace ds2 {
23
24// Log Level
25enum LogLevel {
26 kLogLevelPacket,
27 kLogLevelDebug,
28 kLogLevelInfo,
29 kLogLevelWarning,
30 kLogLevelError,
31 kLogLevelFatal,
32};
33
34LogLevel GetLogLevel();
35void SetLogLevel(LogLevel level);
36void SetLogColorsEnabled(bool enabled);
37std::string const &GetLogOutputFilename();
38void SetLogOutputFilename(std::string const &filename);
39void SetLogOutputStream(FILE *stream);
40
41void Log(int level, char const *classname, char const *funcname,
42 char const *format, ...) DS2_ATTRIBUTE_PRINTF(4, 5);
43
44// Field width is 2 * sizeof(void *) + 2. We need to add 2 because of the `0x`
45// prefix.
46#if defined(BITSIZE_32)
47#define PRI_PTR "#010" PRIxPTR
48#elif defined(BITSIZE_64)
49#define PRI_PTR "#018" PRIxPTR
50#elif defined(BITSIZE_128)
51#define PRI_PTR "#034" PRIxPTR
52#endif
53
54#define PRI_PTR_CAST(VAL) ((uintptr_t)(VAL))
55
56#if defined(COMPILER_MSVC)
57#define FUNCTION_NAME __FUNCTION__
58#elif defined(COMPILER_CLANG) || defined(COMPILER_GCC)
59#define FUNCTION_NAME __PRETTY_FUNCTION__
60#else
61#error "Compiler not supported."
62#endif
63
64#define DS2LOG(LVL, ...) \
65 ds2::Log(ds2::kLogLevel##LVL, nullptr, FUNCTION_NAME, __VA_ARGS__)
66
67#if !defined(NDEBUG)
68#define DS2ASSERT(COND) \
69 do { \
70 if (!(COND)) { \
71 DS2LOG(Fatal, "assertion `%s' failed at %s:%d", #COND, __FILE__, \
72 __LINE__); \
73 DS2_UNREACHABLE(); \
74 } \
75 } while (0)
76#else
77#define DS2ASSERT(COND) \
78 do { \
79 if (!(COND)) \
80 DS2_UNREACHABLE(); \
81 } while (0)
82#endif
83
84#if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
85#define DS2BUG(MESSAGE, ...) \
86 do { \
87 DS2LOG(Fatal, "bug at %s:%d: " MESSAGE, __FILE__, __LINE__, \
88 ##__VA_ARGS__); \
89 DS2_UNREACHABLE(); \
90 } while (0)
91#elif defined(COMPILER_MSVC)
92#define DS2BUG(MESSAGE, ...) \
93 do { \
94 DS2LOG(Fatal, "bug at %s:%d: " MESSAGE, __FILE__, __LINE__, __VA_ARGS__); \
95 DS2_UNREACHABLE(); \
96 } while (0)
97#else
98#error "Compiler not supported."
99#endif
100} // namespace ds2