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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// SPDX-License-Identifier: Apache-2.0

/*
 * Copyright 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 <sqlite3.h>
#include <stdbool.h>
#include <stddef.h>
#include <stumpless/config.h>
#include <stumpless/entry.h>
#include <stumpless/error.h>
#include <stumpless/target.h>
#include <stumpless/target/sqlite3.h>
#include "private/config.h"
#include "private/config/wrapper/locale.h"
#include "private/config/wrapper/get_now.h"
#include "private/config/wrapper/thread_safety.h"
#include "private/entry.h"
#include "private/error.h"
#include "private/facility.h"
#include "private/formatter.h"
#include "private/inthelper.h"
#include "private/memory.h"
#include "private/severity.h"
#include "private/strbuilder.h"
#include "private/target.h"
#include "private/target/sqlite3.h"
#include "private/validate.h"


bool
stumpless_close_sqlite3_target_and_db( const struct stumpless_target *target ) {
  struct sqlite3_target *db_target;
  if( unlikely( !target ) ) {
    raise_argument_empty( L10N_NULL_ARG_ERROR_MESSAGE( "target" ) );
    return false;
  }

  if( target->type != STUMPLESS_SQLITE3_TARGET ) {
    raise_target_incompatible( L10N_INVALID_TARGET_TYPE_ERROR_MESSAGE );
    return false;
  }

  // we use v2 here to prevent close from being blocked by pending transactions
  db_target = target->id;
  int sql_result = sqlite3_close_v2( db_target->db );
  if( sql_result != SQLITE_OK ) {
    raise_sqlite3_failure( L10N_SQLITE3_CLOSE_FAILED_ERROR_MESSAGE,
                           sql_result );
    return false;
  }

  destroy_sqlite3_target( db_target );
  destroy_target( target );
  clear_error(  );
  return true;
}

void
stumpless_close_sqlite3_target_only( const struct stumpless_target *target ) {
  VALIDATE_ARG_NOT_NULL_VOID_RETURN( target );

  if( target->type != STUMPLESS_SQLITE3_TARGET ) {
    raise_target_incompatible( L10N_INVALID_TARGET_TYPE_ERROR_MESSAGE );
    return;
  }

  destroy_sqlite3_target( target->id );
  destroy_target( target );
  clear_error(  );
}

struct stumpless_target *
stumpless_create_default_sqlite3_table( struct stumpless_target *target ) {
  struct sqlite3_target *db_target;
  const char* create_sql = "CREATE TABLE "
                              STUMPLESS_DEFAULT_SQLITE3_TABLE_NAME_STRING " "
                           "( log_id INTEGER PRIMARY KEY,"
                           "  prival INTEGER NOT NULL,"
                           "  version INTEGER NOT NULL,"
                           "  timestamp TEXT,"
                           "  hostname TEXT,"
                           "  app_name TEXT,"
                           "  procid TEXT,"
                           "  msgid TEXT,"
                           "  structured_data TEXT,"
                           "  message TEXT )";
  sqlite3_stmt *create_statement = NULL;
  int sql_result;
  size_t try_count = 0;
  bool busy;
  struct stumpless_target *return_result;

  VALIDATE_ARG_NOT_NULL( target );

  db_target = target->id;
  return_result = target;
  clear_error();

  config_lock_mutex( &db_target->db_mutex );

  sql_result = sqlite3_prepare_v2( db_target->db,
                                   create_sql,
                                   -1,
                                   &create_statement,
                                   NULL );
  if( sql_result != SQLITE_OK ) {
    raise_sqlite3_failure( L10N_SQLITE3_PREPARE_FAILED_ERROR_MESSAGE,
                           sql_result );
    return_result = NULL;
    goto cleanup_and_finish;
  }

  do {
    try_count++;
    sql_result = sqlite3_step( create_statement );

    busy = sql_result == SQLITE_BUSY;
    if( busy && try_count >= STUMPLESS_SQLITE3_RETRY_MAX ) {
      raise_sqlite3_busy();
      return_result = NULL;
      goto cleanup_and_finish;
    }
  } while( busy );

  if( sql_result != SQLITE_DONE ) {
    raise_sqlite3_failure( L10N_SQLITE3_STEP_FAILED_ERROR_MESSAGE,
                           sql_result );
    return_result = NULL;
  }

cleanup_and_finish:
  sqlite3_finalize( create_statement );
  config_unlock_mutex( &db_target->db_mutex );
  return return_result;
}

void *
stumpless_get_sqlite3_db( const struct stumpless_target *target ) {
  struct sqlite3_target *db_target;

  VALIDATE_ARG_NOT_NULL( target );

  if( target->type != STUMPLESS_SQLITE3_TARGET ) {
    raise_target_incompatible( L10N_INVALID_TARGET_TYPE_ERROR_MESSAGE );
    return NULL;
  }

  db_target = target->id;
  return db_target->db;
}

const char *
stumpless_get_sqlite3_insert_sql( const struct stumpless_target *target ) {
  const struct sqlite3_target *db_target;
  const char *result;

  VALIDATE_ARG_NOT_NULL( target );

  db_target = target->id;

  config_lock_mutex( &db_target->db_mutex );
  result = db_target->insert_sql;
  config_unlock_mutex( &db_target->db_mutex );

  return result;
}

stumpless_sqlite3_prepare_func_t
stumpless_get_sqlite3_prepare( const struct stumpless_target *target,
                               void **data ) {
  struct sqlite3_target *db_target;

  VALIDATE_ARG_NOT_NULL( target );

  db_target = target->id;

  if( data ) {
    *data = db_target->prepare_data;
  }

  clear_error();
  return db_target->prepare_func;
}

struct stumpless_target *
stumpless_open_sqlite3_target( const char *name ) {
  int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
  return stumpless_open_sqlite3_target_with_options( name, flags, NULL );
}

struct stumpless_target *
stumpless_open_sqlite3_target_from_db( void *db ) {
  struct stumpless_target *target;

  VALIDATE_ARG_NOT_NULL( db );

  target = new_target( STUMPLESS_SQLITE3_TARGET, "" );

  if( !target ) {
    goto fail;
  }

  target->id = new_sqlite3_target( db );
  if( !target->id ) {
    goto fail_id;
  }

  stumpless_set_current_target( target );
  return target;

fail_id:
  destroy_target( target );
fail:
  return NULL;
}

struct stumpless_target *
stumpless_open_sqlite3_target_with_options( const char *name,
                                            int flags,
                                            const char *vfs ) {
  struct stumpless_target *target;
  sqlite3 *db;
  int sql_result;

  VALIDATE_ARG_NOT_NULL( name );

  target = new_target( STUMPLESS_SQLITE3_TARGET, name );

  if( !target ) {
    goto fail;
  }

  db = NULL;
  sql_result = sqlite3_open_v2( name, &db, flags, vfs );
  if( sql_result != SQLITE_OK ) {
    raise_sqlite3_failure( L10N_SQLITE3_OPEN_FAILED_ERROR_MESSAGE, sql_result );
    goto fail_db;
  }

  target->id = new_sqlite3_target( db );
  if( !target->id ) {
    goto fail_db;
  }

  stumpless_set_current_target( target );
  return target;

fail_db:
  sqlite3_close( db );
  destroy_target( target );
fail:
  return NULL;
}

struct stumpless_target *
stumpless_set_sqlite3_insert_sql( struct stumpless_target *target,
                                  const char *sql ) {
  struct sqlite3_target *db_target;

  VALIDATE_ARG_NOT_NULL( target );

  db_target = target->id;

  config_lock_mutex( &db_target->db_mutex );
  db_target->insert_sql = sql;
  sqlite3_finalize( db_target->insert_stmts[0] );
  db_target->insert_stmts[0] = NULL;
  config_unlock_mutex( &db_target->db_mutex );

  return target;
}

struct stumpless_target *
stumpless_set_sqlite3_prepare( struct stumpless_target *target,
                               stumpless_sqlite3_prepare_func_t preparer,
                               void *data ) {
  struct sqlite3_target *db_target;

  VALIDATE_ARG_NOT_NULL( target );
  VALIDATE_ARG_NOT_NULL( preparer );

  db_target = target->id;

  config_lock_mutex( &db_target->db_mutex );
  db_target->prepare_func = preparer;
  db_target->prepare_data = data;
  config_unlock_mutex( &db_target->db_mutex );

  return target;
}

void *
stumpless_sqlite3_prepare( const struct stumpless_entry *entry,
                           void *data,
                           size_t *count ) {
  struct sqlite3_target *target;
  int sql_result;
  int length;
  sqlite3_stmt *insert_stmt;
  int prival_index;
  int facility_index;
  int severity_index;
  int timestamp_index;
  int hostname_index;
  int app_name_index;
  int procid_index;
  int msgid_index;
  int structured_data_index;
  int message_index;
  char timestamp[RFC_5424_TIMESTAMP_BUFFER_SIZE];
  struct strbuilder *builder;
  const struct strbuilder *strbuilder_result;
  const char *buffer;
  size_t buffer_size;
  const char *msg;

  buffer_size = config_get_now( timestamp );

  target = data;
  if( !target->insert_stmts[0] ) {
    sql_result = sqlite3_prepare_v2( target->db,
                                     target->insert_sql,
                                     -1,
                                     &target->insert_stmts[0],
                                     NULL );
    if( sql_result != SQLITE_OK ) {
      raise_sqlite3_failure( L10N_SQLITE3_PREPARE_FAILED_ERROR_MESSAGE,
                             sql_result );
      return NULL;
    }
  } else {
    sqlite3_reset( target->insert_stmts[0] );
  }

  builder = strbuilder_new();
  if( !builder ) {
    return NULL;
  }

  insert_stmt = target->insert_stmts[0];

  // we can gather the indexes before we need to lock the entry
  prival_index = sqlite3_bind_parameter_index( insert_stmt, "$prival" );
  facility_index = sqlite3_bind_parameter_index( insert_stmt, "$facility" );
  severity_index = sqlite3_bind_parameter_index( insert_stmt, "$severity" );
  timestamp_index = sqlite3_bind_parameter_index( insert_stmt, "$timestamp" );
  hostname_index = sqlite3_bind_parameter_index( insert_stmt, "$hostname" );
  app_name_index = sqlite3_bind_parameter_index( insert_stmt, "$app_name" );
  procid_index = sqlite3_bind_parameter_index( insert_stmt, "$procid" );
  msgid_index = sqlite3_bind_parameter_index( insert_stmt, "$msgid" );
  structured_data_index = sqlite3_bind_parameter_index( insert_stmt,
                                                        "$structured_data" );
  message_index = sqlite3_bind_parameter_index( insert_stmt, "$message" );

  lock_entry( entry );

  if( prival_index != 0 ) {
    sql_result = sqlite3_bind_int( insert_stmt, prival_index, entry->prival );
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$prival" );
      goto fail_bind;
    }
  }

  if( facility_index != 0 ) {
    sql_result = sqlite3_bind_int( insert_stmt,
                                   facility_index,
                                   get_facility( entry->prival ) );
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$facility" );
      goto fail_bind;
    }
  }

  if( severity_index != 0 ) {
    sql_result = sqlite3_bind_int( insert_stmt,
                                   severity_index,
                                   get_severity( entry->prival ) );
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$severity" );
      goto fail_bind;
    }
  }

  if( timestamp_index != 0 ) {
    if( buffer_size == 1 && timestamp[0] == '-'  ) {
      sql_result = sqlite3_bind_null( insert_stmt, timestamp_index );
    } else {
      // transient since it lives on the stack for this function, which will
      // disappear before the step occurs
      sql_result = sqlite3_bind_text( insert_stmt,
                                      timestamp_index,
                                      timestamp,
                                      cap_size_t_to_int( buffer_size ),
                                      SQLITE_TRANSIENT );
    }
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$timestamp" );
      goto fail_bind;
    }
  }

  if( hostname_index != 0 ) {
    strbuilder_result = strbuilder_append_hostname( builder );
    if( !strbuilder_result ) {
      goto fail;
    }
    buffer = strbuilder_get_buffer( builder, &buffer_size );
    if( buffer_size == 1 && buffer[0] == '-' ) {
      sql_result = sqlite3_bind_null( insert_stmt, hostname_index );
    } else {
      // transient since we reuse and destroy this strbuilder before the
      // statement is executed
      sql_result = sqlite3_bind_text( insert_stmt,
                                      hostname_index,
                                      buffer,
                                      cap_size_t_to_int( buffer_size ),
                                      SQLITE_TRANSIENT );
    }
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$hostname" );
      goto fail_bind;
    }
  }

  if( app_name_index != 0 ) {
    if( entry->app_name_length == 1 && entry->app_name[0] == '-' ) {
      sql_result = sqlite3_bind_null( insert_stmt, app_name_index );
    } else {
      length = cap_size_t_to_int( entry->app_name_length );
      sql_result = sqlite3_bind_text( insert_stmt,
                                      app_name_index,
                                      entry->app_name,
                                      length,
                                      SQLITE_STATIC );
    }
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$app_name" );
      goto fail_bind;
    }
  }

  if( procid_index != 0 ) {
    strbuilder_reset( builder );
    strbuilder_result = strbuilder_append_procid( builder );
    if( !strbuilder_result ) {
      goto fail;
    }
    buffer = strbuilder_get_buffer( builder, &buffer_size );
    if( buffer_size == 1 && buffer[0] == '-' ) {
      sql_result = sqlite3_bind_null( insert_stmt, procid_index );
    } else {
      // transient since we reuse and destroy this strbuilder before the
      // statement is executed
      sql_result = sqlite3_bind_text( insert_stmt,
                                      procid_index,
                                      buffer,
                                      cap_size_t_to_int( buffer_size ),
                                      SQLITE_TRANSIENT );
    }
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$procid" );
      goto fail_bind;
    }
  }

  if( msgid_index != 0 ) {
    if( entry->msgid_length == 1 && entry->msgid[0] == '-' ) {
      sql_result = sqlite3_bind_null( insert_stmt, msgid_index );
    } else {
      length = cap_size_t_to_int( entry->msgid_length );
      sql_result = sqlite3_bind_text( insert_stmt,
                                      msgid_index,
                                      entry->msgid,
                                      length,
                                      SQLITE_STATIC );
    }
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$msgid" );
      goto fail_bind;
    }
  }

  if( structured_data_index != 0 ) {
    strbuilder_reset( builder );
    strbuilder_result = strbuilder_append_structured_data( builder, entry );
    if( !strbuilder_result ) {
      goto fail;
    }
    buffer = strbuilder_get_buffer( builder, &buffer_size );
    if( buffer_size == 1 && buffer[0] == '-' ) {
      sql_result = sqlite3_bind_null( insert_stmt, structured_data_index );
    } else {
      // transient since we reuse and destroy this strbuilder before the
      // statement is executed
      sql_result = sqlite3_bind_text( insert_stmt,
                                      structured_data_index,
                                      buffer,
                                      cap_size_t_to_int( buffer_size ),
                                      SQLITE_TRANSIENT );
    }
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$structured_data" );
      goto fail_bind;
    }
  }

  if( message_index != 0 ) {
    if( entry->message ) {
      length = cap_size_t_to_int( entry->message_length );
      sql_result = sqlite3_bind_text( insert_stmt,
                                      message_index,
                                      entry->message,
                                      length,
                                      SQLITE_STATIC );
    } else {
      sql_result = sqlite3_bind_null( insert_stmt, message_index );
    }
    if( sql_result != SQLITE_OK ) {
      msg = L10N_SQLITE3_BIND_FAILED_ERROR_MESSAGE( "$message" );
      goto fail_bind;
    }
  }

  unlock_entry( entry );
  strbuilder_destroy( builder );

  *count = 1;
  return &target->insert_stmts;

fail_bind:
  raise_sqlite3_failure( msg, sql_result );
fail:
  unlock_entry( entry );
  strbuilder_destroy( builder );
  return NULL;
}

/* private definitions */

void
destroy_sqlite3_target( const struct sqlite3_target *target ) {
  sqlite3_finalize( target->insert_stmts[0] );
  config_destroy_mutex( &target->db_mutex );
  free_mem( target );
}

struct sqlite3_target *
new_sqlite3_target( sqlite3 *db ) {
  struct sqlite3_target *target;

  target = alloc_mem( sizeof( *target ) );
  if( !target ) {
    return NULL;
  }

  target->db = db;
  target->insert_sql = STUMPLESS_DEFAULT_SQLITE3_INSERT_SQL;
  target->prepare_func = stumpless_sqlite3_prepare;
  target->prepare_data = target;
  target->insert_stmts[0] = NULL;
  config_init_mutex( &target->db_mutex );

  return target;
}

int
send_entry_to_sqlite3_target( const struct stumpless_target *target,
                              const struct stumpless_entry *entry ) {
  struct sqlite3_target *db_target;
  size_t stmt_count;
  size_t i;
  sqlite3_stmt **statements;
  int result = 1;
  int sql_result;
  size_t try_count = 0;
  bool busy;

  db_target = target->id;

  config_lock_mutex( &db_target->db_mutex );

  statements = db_target->prepare_func( entry,
                                        db_target->prepare_data,
                                        &stmt_count );
  if( !statements ) {
    result = -1;
    if( db_target->prepare_func != &stumpless_sqlite3_prepare ) {
      raise_error( STUMPLESS_SQLITE3_CALLBACK_FAILURE,
                   L10N_SQLITE3_CUSTOM_PREPARE_FAILED_ERROR_MESSAGE,
                   0,
                   NULL );
    }
    goto cleanup_and_finish;
  }

  for( i = 0; i < stmt_count; i++ ) {
    do {
      try_count++;
      sql_result = sqlite3_step( statements[i] );

      busy = sql_result == SQLITE_BUSY;
      if( busy && try_count >= STUMPLESS_SQLITE3_RETRY_MAX ) {
        raise_sqlite3_busy();
        goto cleanup_and_finish;
      }
    } while( busy );

    if( sql_result != SQLITE_DONE ) {
      result = -1;
      raise_sqlite3_failure( L10N_SQLITE3_STEP_FAILED_ERROR_MESSAGE,
                             sql_result );
      goto cleanup_and_finish;
    }
  }

cleanup_and_finish:
  config_unlock_mutex( &db_target->db_mutex );
  return result;
}