File: logging-guidelines.md

package info (click to toggle)
glusterfs 11.2-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 28,244 kB
  • sloc: ansic: 471,238; sh: 45,610; python: 16,893; perl: 3,328; makefile: 2,014; yacc: 487; ruby: 171; lisp: 124; xml: 75; lex: 61
file content (364 lines) | stat: -rw-r--r-- 11,514 bytes parent folder | download
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Guidelines on using the logging framework within a component

Gluster library libglusterfs.so provides message logging abstractions that
are intended to be used across all code/components within gluster.

There could be potentially 2 major cases how the logging infrastructure is
used,

- A new gluster service daemon or end point is created

  - The service daemon infrastructure itself initlializes the logging
    infrastructure (i.e calling gf_log_init and related set functions)

    - See, glusterfsd.c:logging_init

  - Alternatively there could be a case where an end point service (say
    gfapi) may need to do the required initialization

  - This document does not (yet?) cover guidelines for these cases. Best
    bet would be to look at code in glusterfsd.c:logging_init (or equivalent)
    in case a need arises and you reach this document.

- A new xlator or subcomponent is written as a part of the stack

  - Primarily in this case, the consumer of the logging APIs would only
    invoke an API to *log* a particular message at a certain severity

  - This document elaborates on this use of the message logging framework
    in this context

There are 3 interfaces provided to log messages:

1. GF_LOG* structured message interface

   All new messages should be defined using this interface. More details
   about it in the next section.

2. gf_msg* interface

   This interface is deprecated now. New log messages should use the
   new structured interface.

3. gf_log* interface

   This interface was deprecated long ago and it must not be used
   anymore.

## Structured log messages

This interface is designed to be easy to use, flexible and consistent.

The main advantages are:

- **Centralized message definition**

  All messages are defined in a unique location. If a message text needs to be
  updated, only one place has to be changed, even if the same log message is
  used in many places.

- **Customizable list of additional data per message**

  Each message can contain a list of additional info that will be logged as
  part of the message itself. This extra data is:

  - **Declared once**

    It's defined as part of the centralized message definition itself

  - **Typed**

    Each value has a type that is checked by the C compiler at build time to
    ensure correctness.

  - **Enforced**

    Each extra data field needs to be specified when a message of that type
    is logged. If the fields passed when a message is logged doesn't match the
    definition, the compiler will generate an error. This way it's easy to
    identify all places where a message has been used and update them.

- **Better uniformity in data type representation**

  Each data types are represented in the same way in all messages, increasing
  the consistency of the logs.

- **Compile-time generation of messages**

  The text and the extra data is formatted at compile time to reduce run time
  cost.

- **All argument preparation is done only if the message will be logged**

  Data types that need some preprocessing to be logged, are not computed until
  we are sure that the message needs to be logged based on the current log
  level.

- **Very easy to use**

  Definition of messages and its utilization is quite simple. There are some
  predefined types, but it's easy to create new data types if needed.

- **Code auto-completion friendly**

  Once a message is defined, logging it is very simple when an IDE with code
  auto-completion is used. The code auto-completion will help to find the
  name of the message and the list of arguments it needs.

- **All extra overhead is optimally optimized by gcc/clang**

  The additional code and structures required to make all this possible are
  easily optimized by compilers, so resulting code is equivalent to directly
  logging the message.

### Definition of new messages

All messages at log level INFO or above need to be declared inside a header
file. They will be assigned a unique identifier that will appear in the logs
so that specific messages can be easily located even if the text description
changes.

For DEBUG and TRACE messages, we don't assign a unique identifier to them and
the message is defined in-place where it's used with a very similar format.

#### Creating a new component

If a new xlator or component is created that requires some messages, the first
thing to do is to reserve a component ID in file glusterfs/glfs-message-id.h.

This is done by adding a new `GLFS_MSGID_COMP()` entry at the end of the
`enum _msgid_comp`. A unique name and a number of blocks to reserve must
be specified (each block can contain up to 1000 messages).

Example:

> ```c
>     GLFS_MSGID_COMP(EXAMPLE, 1),
>     /* --- new segments for messages goes above this line --- */
>
>     GLFS_MSGID_END
> ```

Once created, a copy of glusterfs/template-component-messages.h can be used as
a starting point for the messages of the new component. Check the comments of
that file for more information, but basically you need to use the macro
`GLFS_COMPONENT()` before starting defining the messages.

Example:

> ```c
> GLFS_COMPONENT(EXAMPLE);
> ```

#### Creating new messages

Each message is automatically assigned a unique sequential number and it
should remain the same once created. This means that you must create new
messages at the end of the file, after any other message. This way the newly
created message will take the next free sequential id, without touching any
previously assigned id.

To define a message, the macro `GLFS_NEW()` must be used. It requires four
mandatory arguments:

1. The name of the component. This is the one created in the previous section.

2. The name of the message. This is the name to use when you want to log the
   message.

3. The text associated to the message. This must be a fixed string without
   any formatting.

4. The number of extra data fields to include to the message.

If there are extra data fields, for each field you must add field definition
inside the macro.

For debug and trace logs, messages are not predefined. Wherever a these
messages are used, the definition of the message itself is used instead of
the name of the message.

##### Field definitions

Each field consists of five arguments, written between parenthesis:

1. **Data type**

   This is a regular C type that will be used to manipulate the data. It can
   be anything valid.

2. **Field name**

   This is the name that will be used to reference the data and to show it in
   the log message. It must be a valid C identifier.

3. **Data source**

   This is only used for in-place messages. It's a simple piece of code to
   access the data. It can be just a variable name or something a bit more
   complex like a structure access or even a function call returning a value.

4. **Format string**

   This is a string representing the way in which this data will be shown in
   the log. It can be something as simple as '%u' or a bit more elaborated
   like '%d (%s)', depending on how we want to show something.

5. **Format data**

   This must be a list of expressions to generate each of the arguments needed
   for the format string. In most cases this will be just the name of the
   field, but it could be something else if the data needs to be processed.

6. **Preparation code**

   This is optional. If present it must contain any additional variable
   definition and code to prepare the format data.

Examples for message definitions:

> ```c
>     (uint32_t, value, , "%u", (value))
> ```
>
> ```c
>     (int32_t, error, , "%d (%s)", (error, strerror(error)))
> ```
>
> ```c
>     (uuid_t *, gfid, , "%s", (gfid_str),
>      char gfid_str[48]; uuid_unparse(*gfid, gfid_str))
> ```

Examples for in-place messages:

> ```c
>     (uint32_t, value, data->count, "%u", (value))
> ```
>
> ```c
>     (int32_t, error, errno, "%d (%s)", (error, strerror(error)))
> ```
>
> ```c
>     (uuid_t *, gfid, &inode->gfid, "%s", (gfid_str),
>      char gfid_str[48]; uuid_unparse(*gfid, gfid_str))
> ```

##### Predefined data types

Some macros are available to declare typical data types and make them easier
to use:

- Signed integers: `GLFS_INT(name [, src])`
- Unsigned integers: `GLFS_UINT(name [, src])`
- Errors:
  - Positive errors: `GLFS_ERR(name [, src])`
  - Negative errors: `GLFS_RES(name [, src])`
- Strings: `GLFS_STR(name [, src])`
- UUIDs: `GLFS_UUID(name [, src])`
- Pointers: `GLFS_PTR(name [, src])`

The `src` argument is only used for in-place messages.

This is a full example that defines a new message using the previous macros:

```c
GLFS_NEW(EXAMPLE, MSG_TEST, "This is a test message", 3,
    GLFS_UINT(number),
    GLFS_STR(name),
    GLFS_ERR(error)
)
```

This will generate a log message with the following format:

```c
"This is a test message <{number=%u}, {name='%s'}, {error=%d (%s)}>"
```

#### Logging messages

Once a message is defined, it can be logged using the following macros:

- `GF_LOG_C()`: log a critical message
- `GF_LOG_E()`: log an error message
- `GF_LOG_W()`: log a warning message
- `GF_LOG_I()`: log an info message
- `GF_LOG_D()`: log a debug message
- `GF_LOG_T()`: log a trace message

All macros receive a string, representing the domain of the log message. For
INFO or higher messages, the name of the messages is passed, including all
additional data between parenthesis. In case of DEBUG and TRACE messages, a
message definition follows.

Example:

> ```c
>     GF_LOG_I(this->name, MSG_TEST(10, "something", ENOENT));
> ```

The resulting logging message would be similar to this:

```c
"This is a test message <{number=10}, {name='something'}, {error=2 (File not found)}>"
```

A similar example with a debug message:

> ```c
>     GF_LOG_D(this->name, "Debug message",
>         GLFS_UINT(number, data->value),
>         GLFS_STR(name),
>         GLFS_ERR(error, op_errno)
>     );

Note that if the field name matches the source of the data as in the case of
the second field, the source argument can be omitted.

## Migration from older interfaces

Given the amount of existing messages, it's not feasible to migrate all of
them at once, so a special macro is provided to allow incremental migration
of existing log messages.

1. Migrate header file

   The first step is to update the header file where all message IDs are
   defined.

   - Initialize the component

     You need to add the `GLFS_COMPONENT()` macro at the beginning with the
     appropriate component name. This name can be found in the first argument
     of the existing `GLFS_MSGID()` macro.

   - Replace message definitions

     All existing messages inside `GLFS_MSGID()` need to be converted to:

     ```c
     GLFS_MIG(component, id, "", 0)
     ```

     Where `component` is the name of the component used in `GLFS_COMPONENT()`,
     and `id` is each of the existing IDs inside `GLFS_MSGID()`.

     This step will use the new way of defining messages, but is compatible
     with the old logging interface, so once this is done, the code should
     compile fine.

2. Migrate a message

   It's possible to migrate the messages one by one without breaking anything.

   For each message to migrate:

   - Choose one message.
   - Replace `GLFS_MIG` by `GLFS_NEW`.
   - Add a meaningful message text as the third argument.
   - Update the number of fields if necessary.
   - Add the required field definition.
   - Look for each instance of the log message in the code.
   - Replace the existing log macro by one of the `GF_LOG_*()` macros.