hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
// Copyright 2019 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef __MINILOG_LOG_H
#define __MINILOG_LOG_H
 
#ifdef __APPLE__
#define SECTION(X) section("__DATA,__" X)
#define SECTION_START(X) __asm("section$start$__DATA$__" X)
#define SECTION_END(X) __asm("section$end$__DATA$__" X)
#else
#define SECTION(X) section(X)
#define SECTION_START(X)
#define SECTION_END(X)
#endif
 
#ifdef __cplusplus
extern "C" {
#endif
 
#include <stdbool.h>
 
struct minilog_debug_desc {
  const char *name;
  const char *file;
#define MINILOG_DEBUG_FLAG_DEFAULT (0)
#define MINILOG_DEBUG_FLAG_PRINT (1 << 0)
#define MINILOG_DEBUG_FLAG_ALIAS (1 << 1)
  unsigned int flags;
} __attribute__((aligned(8)));
 
int __minilog_log_init(const char *program, const char *debug, bool detach,
                     bool backtrace, const char *program_name,
                     const char *program_version);
void __minilog_log_cleanup(bool backtrace);
void __minilog_log_enable(struct minilog_debug_desc *start,
                        struct minilog_debug_desc *stop);
 
void minilog_info(const char *format, ...) __attribute__((format(printf, 1, 2)));
void minilog_warn(const char *format, ...) __attribute__((format(printf, 1, 2)));
void minilog_error(const char *format, ...) __attribute__((format(printf, 1, 2)));
void minilog_debug(const char *format, ...) __attribute__((format(printf, 1, 2)));
 
#define minilog_warn_once(fmt, ...)                                           \
  do {                                                                         \
    static bool printed;                                                       \
    if (!printed) {                                                            \
      minilog_warn(fmt, __VA_ARGS__);                                                  \
      printed = true;                                                          \
    }                                                                          \
  } while (0)
 
#define MINILOG_DEBUG_DEFINE(name)                                               \
  static struct minilog_debug_desc __debug_alias_##name                          \
      __attribute__((used, SECTION("__debug"), aligned(8))) = {                \
          #name, __FILE__, MINILOG_DEBUG_FLAG_ALIAS};
 
#define DBG(fmt, ...)                                                       \
  do {                                                                         \
    static struct minilog_debug_desc __minilog_debug_desc                          \
        __attribute__((used, SECTION("__debug"), aligned(8))) = {              \
            .file = __FILE__,                                                  \
            .flags = MINILOG_DEBUG_FLAG_DEFAULT,                                 \
    };                                                                         \
    if (__minilog_debug_desc.flags & MINILOG_DEBUG_FLAG_PRINT)                     \
      minilog_debug("%s:%s() " fmt, __FILE__, __FUNCTION__, __VA_ARGS__);              \
  } while (0)
 
#ifdef __cplusplus
}
#endif
 
#endif /* __MINILOG_LOG_H */