stumpless-sys 0.3.0

Bindings for the Stumpless logging library.
Documentation
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// SPDX-License-Identifier: Apache-2.0

/*
 * Copyright 2018-2023 Joel E. Anderson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stumpless/config.h>
#include <stumpless/error.h>
#include "private/config/wrapper/locale.h"
#include "private/config/wrapper/thread_safety.h"
#include "private/error.h"
#include "private/inthelper.h"
#include "private/strhelper.h"

/* global static variables */
static config_atomic_ptr_t error_stream = config_atomic_ptr_initializer;
static config_atomic_bool_t error_stream_free = config_atomic_bool_true;
static config_atomic_bool_t error_stream_valid = config_atomic_bool_false;

static const char *error_enum_to_string[] = {
  STUMPLESS_FOREACH_ERROR( GENERATE_STRING )
};

/* per-thread static variables */
static CONFIG_THREAD_LOCAL_STORAGE struct stumpless_error last_error;
static CONFIG_THREAD_LOCAL_STORAGE bool error_valid = false;

const struct stumpless_error *
stumpless_get_error( void ) {
  if( error_valid )
    return &last_error;
  else
    return NULL;
}

enum stumpless_error_id
stumpless_get_error_id( const struct stumpless_error *err ) {
  return err->id;
}

const char *
stumpless_get_error_id_string( enum stumpless_error_id id) {
  int error_id_upper_bound = sizeof( error_enum_to_string ) / sizeof( char * );
  if ( id >= 0 && id < error_id_upper_bound ) {
    return error_enum_to_string[id];
  }

  return "NO_SUCH_ERROR_ID";
}

FILE *
stumpless_get_error_stream( void ) {
  if( config_read_bool( &error_stream_valid ) ) {
    return config_read_ptr( &error_stream );
  } else {
    return stderr;
  }
}

bool
stumpless_has_error( void ) {
  return error_valid;
}

void
stumpless_perror( const char *prefix ) {
  FILE *stream;
  bool locked;

  stream = stumpless_get_error_stream(  );

  if( stream && error_valid ) {

    do {
      locked = config_compare_exchange_bool( &error_stream_free, true, false );
    } while( !locked );

    if( prefix ) {
      fputs( prefix, stream );
      fputc( ':', stream );
      fputc( ' ', stream );
    }

    fputs( stumpless_get_error_id_string(last_error.id), stream );
    fputc( ':', stream );
    fputc( ' ', stream );

    fputs( last_error.message, stream );

    if( last_error.code_type ) {
      fprintf( stream, " (%s: %d)", last_error.code_type, last_error.code );
    }

    fputc( '\n', stream );
  }

  config_write_bool( &error_stream_free, true );
}

void
stumpless_set_error_stream( FILE *stream ) {
  config_write_ptr( &error_stream, stream );
  config_write_bool( &error_stream_valid, true );
}

/* private functions */

/**
 * Clears the internal error state. This must be called at some point in all
 * public functions to make sure that residual errors don't stick around.
 *
 * If a public function calls another public function internally, it can skip
 * calling this function as the called public function will clear the error if
 * it succeeds, and should pass the error on if not.
 *
 * Ideally, this should be called directly after the last possible failure point
 * in the function to avoid unnecessary clears, but before other code in case a
 * non-fatal error is raised and needs to be visible upon return.
 */
void
clear_error( void ) {
  error_valid = false;
}

void
raise_address_failure( const char *message, int code, const char *code_type ) {
  raise_error( STUMPLESS_ADDRESS_FAILURE, message, code, code_type );
}

void
raise_argument_empty( const char *message ) {
  raise_error( STUMPLESS_ARGUMENT_EMPTY, message, 0, NULL );
}

void
raise_argument_too_big( const char *message,
                        size_t arg_size,
                        const char *arg_type ) {
  raise_error( STUMPLESS_ARGUMENT_TOO_BIG,
               message,
               cap_size_t_to_int( arg_size ),
               arg_type );
}

void
raise_duplicate_element( void ) {
  raise_error( STUMPLESS_DUPLICATE_ELEMENT,
               L10N_DUPLICATE_ELEMENT_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_element_not_found( void ) {
  raise_error( STUMPLESS_ELEMENT_NOT_FOUND,
               L10N_ELEMENT_NOT_FOUND_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_error( enum stumpless_error_id id,
             const char *message,
             int code,
             const char *code_type ) {
  last_error.id = id;
  last_error.message = message;
  last_error.code = code;
  last_error.code_type = code_type;
  error_valid = true;
}

void
raise_file_open_failure( void ) {
  raise_error( STUMPLESS_FILE_OPEN_FAILURE,
               L10N_FILE_OPEN_FAILURE_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_file_write_failure( void ) {
  raise_error( STUMPLESS_FILE_WRITE_FAILURE,
               L10N_FILE_WRITE_FAILURE_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_function_target_failure( int code ) {
  raise_error( STUMPLESS_FUNCTION_TARGET_FAILURE,
               L10N_FUNCTION_TARGET_FAILURE_ERROR_MESSAGE,
               code,
               L10N_FUNCTION_TARGET_FAILURE_CODE_TYPE );
}

void
raise_gethostname_failure( const char *message,
                           int code,
                           const char *code_type ) {
  raise_error( STUMPLESS_GETHOSTNAME_FAILURE, message, code, code_type );
}

void
raise_index_out_of_bounds( const char *message, size_t index ) {
  raise_error( STUMPLESS_INDEX_OUT_OF_BOUNDS,
               message,
               size_t_to_int( index ),
               L10N_INDEX_OUT_OF_BOUNDS_ERROR_CODE_TYPE );
}

void
raise_invalid_encoding( const char *message ) {
  raise_error( STUMPLESS_INVALID_ENCODING, message, 0, NULL );
}

void
raise_invalid_facility( int facility ) {
  raise_error( STUMPLESS_INVALID_FACILITY,
               L10N_INVALID_FACILITY_ERROR_MESSAGE,
               facility,
               L10N_INVALID_FACILITY_ERROR_CODE_TYPE );
}

void
raise_invalid_id( void ) {
  raise_error( STUMPLESS_INVALID_ID,
               L10N_INVALID_ID_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_invalid_severity( int severity ) {
  raise_error( STUMPLESS_INVALID_SEVERITY,
               L10N_INVALID_SEVERITY_ERROR_MESSAGE,
               severity,
               L10N_INVALID_SEVERITY_ERROR_CODE_TYPE );
}

void
raise_journald_failure( int code ) {
  raise_error( STUMPLESS_JOURNALD_FAILURE,
               L10N_JOURNALD_FAILURE_ERROR_MESSAGE,
               code,
               L10N_JOURNALD_FAILURE_ERROR_CODE_TYPE );
}

void
raise_mb_conversion_failure( int code ) {
  raise_error( STUMPLESS_INVALID_ENCODING,
               L10N_MB_TO_WIDE_CONVERSION_ERROR_MESSAGE,
               code,
               L10N_MB_TO_WIDE_CONVERSION_ERROR_CODE_TYPE );
}

void
raise_memory_allocation_failure( void ) {
  raise_error( STUMPLESS_MEMORY_ALLOCATION_FAILURE,
               L10N_MEMORY_ALLOCATION_FAILURE_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_network_closed( const char *message ) {
  raise_error( STUMPLESS_NETWORK_CLOSED, message, 0, NULL );
}

void
raise_network_protocol_unsupported( void ) {
  raise_error( STUMPLESS_NETWORK_PROTOCOL_UNSUPPORTED,
               L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_param_not_found( void ) {
  raise_error( STUMPLESS_PARAM_NOT_FOUND,
               L10N_PARAM_NOT_FOUND_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_resolve_hostname_failure( const char *message ) {
  raise_error( STUMPLESS_ADDRESS_FAILURE,
               message,
               0,
               NULL );
}

void
raise_socket_bind_failure( const char *message,
                           int code,
                           const char *code_type ) {
  raise_error( STUMPLESS_SOCKET_BIND_FAILURE, message, code, code_type );
}

void
raise_socket_connect_failure( const char *message,
                              int code,
                              const char *code_type ) {
  raise_error( STUMPLESS_SOCKET_CONNECT_FAILURE, message, code, code_type );
}

void
raise_socket_failure( const char *message, int code, const char *code_type ) {
  raise_error( STUMPLESS_SOCKET_FAILURE, message, code, code_type );
}

void
raise_socket_send_failure( const char *message,
                           int code,
                           const char *code_type ) {
  raise_error( STUMPLESS_SOCKET_SEND_FAILURE, message, code, code_type );
}

void
raise_sqlite3_busy( void ) {
  raise_error( STUMPLESS_SQLITE3_BUSY,
               L10N_SQLITE3_BUSY_ERROR_MESSAGE,
               STUMPLESS_SQLITE3_RETRY_MAX,
               L10N_SQLITE3_RETRY_COUNT_CODE_TYPE );
}

void
raise_sqlite3_failure( const char *message, int code ) {
  raise_error( STUMPLESS_SQLITE3_FAILURE,
               message,
               code,
               L10N_SQLITE3_RESULT_CODE_TYPE );
}

void
raise_stream_write_failure( void ) {
  raise_error( STUMPLESS_STREAM_WRITE_FAILURE,
               L10N_STREAM_WRITE_FAILURE_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_target_incompatible( const char *message ) {
  raise_error( STUMPLESS_TARGET_INCOMPATIBLE, message, 0, NULL );
}

void
raise_target_unsupported( const char *message ) {
  raise_error( STUMPLESS_TARGET_UNSUPPORTED, message, 0, NULL );
}

void
raise_transport_protocol_unsupported( void ) {
  raise_error( STUMPLESS_TRANSPORT_PROTOCOL_UNSUPPORTED,
               L10N_TRANSPORT_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_wel_close_failure( void ) {
  raise_error( STUMPLESS_WINDOWS_EVENT_LOG_CLOSE_FAILURE,
               L10N_WEL_CLOSE_FAILURE_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_wel_open_failure( void ) {
  raise_error( STUMPLESS_WINDOWS_EVENT_LOG_OPEN_FAILURE,
               L10N_WEL_OPEN_FAILURE_ERROR_MESSAGE,
               0,
               NULL );
}

void
raise_wide_conversion_failure( int code, const char *code_type ) {
  raise_error( STUMPLESS_INVALID_ENCODING,
    L10N_WIDE_TO_MB_CONVERSION_ERROR_MESSAGE,
    code,
    code_type );
}

void
raise_windows_failure( const char *message, int code, const char *code_type ) {
  raise_error( STUMPLESS_WINDOWS_FAILURE, message, code, code_type );
}

void
write_to_error_stream( const char *msg, size_t msg_size ) {
  FILE *stream;
  bool locked;

  stream = stumpless_get_error_stream(  );
  if( !stream ) {
    return;
  }

  do{
    locked = config_compare_exchange_bool( &error_stream_free, true, false );
  } while( !locked );

  fwrite( msg, sizeof( *msg ), msg_size, stream );

  config_write_bool( &error_stream_free, true );
}

void
raise_invalid_param( void ) {
  raise_error( STUMPLESS_INVALID_PARAM_STRING,
               L10N_INVALID_PARAM_ERROR_MESSAGE,
               0,
               NULL );
}