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);
39
40void Log(int level, char const *classname, char const *funcname,
41 char const *format, ...) DS2_ATTRIBUTE_PRINTF(4, 5);
42
43// Field width is 2 * sizeof(void *) + 2. We need to add 2 because of the `0x`
44// prefix.
45#if defined(BITSIZE_32)
46#define PRI_PTR "#010" PRIxPTR
47#elif defined(BITSIZE_64)
48#define PRI_PTR "#018" PRIxPTR
49#elif defined(BITSIZE_128)
50#define PRI_PTR "#034" PRIxPTR
51#endif
52
53#define PRI_PTR_CAST(VAL) ((uintptr_t)(VAL))
54
55#if defined(COMPILER_MSVC)
56#define FUNCTION_NAME __FUNCTION__
57#elif defined(COMPILER_CLANG) || defined(COMPILER_GCC)
58#define FUNCTION_NAME __PRETTY_FUNCTION__
59#else
60#error "Compiler not supported."
61#endif
62
63#define DS2LOG(LVL, ...) \
64 ds2::Log(ds2::kLogLevel##LVL, nullptr, FUNCTION_NAME, __VA_ARGS__)
65
66#if !defined(NDEBUG)
67#define DS2ASSERT(COND) \
68 do { \
69 if (!(COND)) { \
70 DS2LOG(Fatal, "assertion `%s' failed at %s:%d", #COND, __FILE__, \
71 __LINE__); \
72 DS2_UNREACHABLE(); \
73 } \
74 } while (0)
75#else
76#define DS2ASSERT(COND) \
77 do { \
78 if (!(COND)) \
79 DS2_UNREACHABLE(); \
80 } while (0)
81#endif
82
83#if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
84#define DS2BUG(MESSAGE, ...) \
85 do { \
86 DS2LOG(Fatal, "bug at %s:%d: " MESSAGE, __FILE__, __LINE__, \
87 ##__VA_ARGS__); \
88 DS2_UNREACHABLE(); \
89 } while (0)
90#elif defined(COMPILER_MSVC)
91#define DS2BUG(MESSAGE, ...) \
92 do { \
93 DS2LOG(Fatal, "bug at %s:%d: " MESSAGE, __FILE__, __LINE__, __VA_ARGS__); \
94 DS2_UNREACHABLE(); \
95 } while (0)
96#else
97#error "Compiler not supported."
98#endif
99} // namespace ds2