aboutsummaryrefslogtreecommitdiff
path: root/backend/message.h
blob: 622f2a195ee3a94840b6a69213a662cfb0f1fbf7 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*  message.h -- consistent error, progress and debugging feedback
 *  Copyright (C) 2019 SEIKO EPSON Corporation
 *
 *  License: EPSON END USER SOFTWARE LICENSE
 *  Author : SEIKO EPSON Corporation
 *
 *  This file is part of Image Scan! for Linux.
 *  It is distributed under the terms of the EPSON END USER SOFTWARE LICENSE.
 *
 *  You should have received a verbatim copy of the EPSON END USER SOFTWARE
 *  LICENSE along with the software.
 */


#ifndef message_h
#define message_h

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>

#ifndef ENABLE_DEBUG
#define ENABLE_DEBUG 0
#endif

#ifdef __cplusplus
extern "C"
{
#endif


  typedef enum
    {
      ERR_FATAL = (1 << 0),
      ERR_MAJOR = (1 << 1),
      ERR_MINOR = (1 << 2),

      LOG_INFO  = (1 << 3),
      LOG_CALL  = (1 << 4),
      LOG_DATA  = (1 << 5),

      DBG_CMD   = (1 << 6),
      DBG_HEX   = (1 << 7),
      DBG_IMG   = (1 << 8),
    }
    msg_level_type;


  extern unsigned long msg_level;


  /*! \brief Maximum buffer size not considered to be image data
   *
   *  This value can be used by low-level I/O functions that want to
   *  log the interesting data they handle but lack the knowledge to
   *  distinguish between that data and boring old image bytes.
   */
#define MSG_DBG_IMG_THRESHOLD 512


#define FMT_FILE __FILE__ ":%d: "
#define FMT_LINE , __LINE__
#define FMT_MODULE FMT_FILE "[%s]"

#define FMT_FATAL  FMT_MODULE "[F] "
#define FMT_MAJOR  FMT_MODULE "[M] "
#define FMT_MINOR  FMT_MODULE "[m] "
#define FMT_INFO   FMT_MODULE "{I} "
#define FMT_CALL   FMT_MODULE "{C} " "%s "
#define FMT_DATA   FMT_MODULE "{D} "
#define FMT_CMD    FMT_MODULE "(e) "
#define FMT_HEX    "[" MSG_MODULE "]" "(x) "
#define FMT_IMG    "[" MSG_MODULE "]" "(i) "

#if ENABLE_DEBUG

#define err_fatal(fmt,arg...)                                      \
  do                                                               \
    {                                                              \
      if (ERR_FATAL <= msg_level)                                  \
        fprintf (stderr, FMT_FATAL fmt "\n" FMT_LINE, MSG_MODULE,  \
                 ## arg);                                          \
    }                                                              \
  while (0)                                                        \
    /**/

#define err_major(fmt,arg...)                                      \
  do                                                               \
    {                                                              \
      if (ERR_MAJOR <= msg_level)                                  \
        fprintf (stderr, FMT_MAJOR fmt "\n" FMT_LINE, MSG_MODULE,  \
                 ## arg);                                          \
    }                                                              \
  while (0)                                                        \
    /**/

#define err_minor(fmt,arg...)                                      \
  do                                                               \
    {                                                              \
      if (ERR_MINOR <= msg_level)                                  \
        fprintf (stderr, FMT_MINOR fmt "\n" FMT_LINE, MSG_MODULE,  \
                 ## arg);                                          \
    }                                                              \
  while (0)                                                        \
    /**/

#define log_info(fmt,arg...)                                       \
  do                                                               \
    {                                                              \
      if (LOG_INFO <= msg_level)                                   \
        fprintf (stderr, FMT_INFO fmt "\n" FMT_LINE, MSG_MODULE,   \
                 ## arg);                                          \
    }                                                              \
  while (0)                                                        \
    /**/

#define log_call(fmt,arg...)                                       \
  do                                                               \
    {                                                              \
      if (LOG_CALL <= msg_level)                                   \
        fprintf (stderr, FMT_CALL fmt "\n" FMT_LINE, MSG_MODULE,   \
                 __func__, ## arg);                                \
    }                                                              \
  while (0)                                                        \
    /**/

#define log_data(fmt,arg...)                                       \
  do                                                               \
    {                                                              \
      if (LOG_DATA <= msg_level)                                   \
        fprintf (stderr, FMT_DATA fmt "\n" FMT_LINE, MSG_MODULE,   \
                 ## arg);                                          \
    }                                                              \
  while (0)                                                        \
    /**/

#define dbg_cmd(buf,sz)                                            \
  do                                                               \
    {                                                              \
      if (DBG_CMD <= msg_level)                                    \
        fprintf (stderr, FMT_CMD fmt "\n" FMT_LINE, MSG_MODULE,    \
                 ## arg);                                          \
    }                                                              \
  while (0)                                                        \
    /**/

#define dbg_hex(buf,sz)                                            \
  do                                                               \
    {                                                              \
      if (DBG_HEX <= msg_level)                                    \
        msg_dump (FMT_HEX, buf, sz);                               \
    }                                                              \
  while (0)                                                        \
    /**/

#define dbg_img(buf,sz)                                            \
  do                                                               \
    {                                                              \
      if (DBG_IMG <= msg_level)                                    \
        msg_dump (FMT_IMG, buf, sz);                               \
    }                                                              \
  while (0)                                                        \
    /**/

  void msg_init (void);
  void msg_dump (const char *, const void *, size_t);

#else /* !ENABLE_DEBUG */

#define err_fatal(fmt,arg...)
#define err_major(fmt,arg...)
#define err_minor(fmt,arg...)
#define log_info(fmt,arg...)
#define log_call(fmt,arg...)
#define log_data(fmt,arg...)
#define dbg_cmd(buf,sz)
#define dbg_hex(buf,sz)
#define dbg_img(buf,sz)

#define msg_init()
#define msg_dump(fmt,buf,sz)

#endif /* !ENABLE_DEBUG */

#ifdef __cplusplus
}       /* extern "C" */
#endif

#endif  /* !defined (message_h) */