Add dlog_sec_print test
[platform/core/system/dlog.git] / src / libdlog / log.c
1 /*  MIT License
2  *
3  * Copyright (c) 2012-2020 Samsung Electronics Co., Ltd
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is furnished
10  * to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE. */
22
23 // C
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 // POSIX
30 #include <pthread.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33
34 // DLog
35 #include "deduplicate.h"
36 #include <dynamic_config.h>
37 #include <libdlog.h>
38 #include <logcommon.h>
39 #include "logconfig.h"
40 #include "loglimiter.h"
41
42 #define DEFAULT_CONFIG_LIMITER false
43 #define DEFAULT_CONFIG_PLOG true
44 #define DEFAULT_CONFIG_DEBUGMODE 0
45 #define DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS 0
46
47 /**
48  * @brief Points to a function which writes a log message
49  * @details The function pointed to depends on the backend used
50  * @param[in] log_id ID of the buffer to log to. Belongs to (LOG_ID_INVALID, LOG_ID_MAX) non-inclusive
51  * @param[in] prio Priority of the message.
52  * @param[in] tag The message tag, identifies the sender.
53  * @param[in] msg The contents of the message.
54  * @return Returns the number of bytes written on success and a negative error value on error.
55  * @see __dlog_init_backend
56  */
57 int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono) = NULL;
58 void (*destroy_backend)();
59
60 pthread_rwlock_t log_limiter_lock = PTHREAD_RWLOCK_INITIALIZER;
61 static pthread_mutex_t log_construction_lock = PTHREAD_MUTEX_INITIALIZER;
62 static bool is_initialized = false;
63
64 extern void __dlog_init_pipe(const struct log_config *conf);
65 extern void __dlog_init_android(const struct log_config *conf);
66
67 bool limiter;
68 static bool dynamic_config;
69 static bool plog[LOG_ID_MAX];
70 static bool plog_default_values[LOG_ID_MAX];
71 static bool enable_secure_logs = true;
72
73 static int debugmode;
74 static int fatal_assert;
75 static int limiter_apply_to_all_buffers;
76 static _Atomic log_priority priority_filter_level = DLOG_VERBOSE;
77
78 static void __configure_limiter(struct log_config *config)
79 {
80         assert(config);
81
82         if (!limiter)
83                 return;
84
85         limiter = __log_limiter_create(config);
86 }
87
88 static int __configure_backend(struct log_config *config)
89 {
90         assert(config);
91
92         const char *const backend = log_config_get(config, "backend");
93         if (!backend)
94                 return 0;
95
96         if (!strcmp(backend, "pipe"))
97                 __dlog_init_pipe(config);
98         else if (!strcmp(backend, "logger"))
99                 __dlog_init_android(config);
100         else
101                 return 0;
102
103         return 1;
104 }
105
106 static void __set_plog_default_values()
107 {
108         for (size_t i = 0; i < NELEMS(plog); ++i)
109                 plog_default_values[i] = plog[i];
110 }
111
112 static void __initialize_plog(const struct log_config *config)
113 {
114         assert(config);
115
116         const bool plog_default = log_config_get_boolean(config, "plog", DEFAULT_CONFIG_PLOG);
117         for (size_t i = 0; i < NELEMS(plog); ++i)
118                 plog[i] = plog_default;
119         plog[LOG_ID_APPS] = true; // the default does not apply here for backward compatibility reasons.
120         __set_plog_default_values();
121 }
122
123 static void __configure_parameters(struct log_config *config)
124 {
125         assert(config);
126
127         __initialize_plog(config);
128         __update_plog(config);
129         __set_plog_default_values();
130
131         enable_secure_logs = log_config_get_boolean(config, "enable_secure_logs", enable_secure_logs);
132         debugmode = log_config_get_int(config, "debugmode", DEFAULT_CONFIG_DEBUGMODE);
133         fatal_assert = access(DEBUGMODE_FILE, F_OK) != -1;
134         limiter = log_config_get_boolean(config, "limiter", DEFAULT_CONFIG_LIMITER);
135         limiter_apply_to_all_buffers = log_config_get_int(config,
136                                                                         "limiter_apply_to_all_buffers",
137                                                                         DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS);
138 }
139
140 void __update_plog(const struct log_config *conf)
141 {
142         assert(conf);
143
144         for (size_t i = 0; i < NELEMS(plog); ++i) {
145                 char key[MAX_CONF_KEY_LEN];
146                 const int r = snprintf(key, sizeof key, "enable_%s", log_name_by_id((log_id_t)i));
147                 if (r < 0)
148                         continue;
149                 plog[i] = log_config_get_boolean(conf, key, plog_default_values[i]);
150         }
151 }
152
153 /**
154  * @brief Configure the library
155  * @details Reads relevant config values
156  * @remarks This is more or less a constructor, but there are some obstacles
157  *          to using it as such (i.e. with attribute constructor):
158  *
159  *  - some important pieces of the system link to dlog, they start very early
160  *    such that dlog can't properly initialize (which lasts for program lifetime)
161  *    but don't actually log anything until later on and would be fine under lazy
162  *    initialisation. The way to do it "properly" would be to expose this function
163  *    into the API so that people can manually call it when they're ready, but
164  *    one of the design goals of the current API is that it requires absolutely no
165  *    other calls than `dlog_print`. Changing it would require somebody with a
166  *    bird's eye view of the system to produce a design so I wouldn't count on it.
167  *
168  *  - the constructor would need to have as high of a priority as possible (so as
169  *    to minimize the risk of another library's constructor using uninitialized data)
170  *    but at the same time others might want some room to wrap functions before
171  *    dlog uses them (think mprobe/mcheck). This would also require a design pass.
172  */
173 #ifndef UNIT_TEST
174 static
175 #endif
176 bool __configure(void)
177 {
178         __attribute__((cleanup(log_config_free))) struct log_config config;
179
180         if (log_config_read(&config) < 0)
181                 return false;
182
183         dynamic_config = __dynamic_config_create(&config);
184
185         __configure_parameters(&config);
186
187         if (!__configure_backend(&config)) {
188                 __dynamic_config_destroy();
189                 dynamic_config = false;
190                 return false;
191         }
192
193         __configure_deduplicate(&config);
194         __configure_limiter(&config);
195         return true;
196 }
197
198 static void __attribute__((constructor(101))) __install_pipe_handler(void)
199 {
200         /* We mask SIGPIPE signal because most applications do not install their
201          * own SIGPIPE handler. Default behaviour in SIGPIPE case is to abort the
202          * process. SIGPIPE occurs when e.g. dlog daemon closes read pipe endpoint.
203          *
204          * We do this in the library constructor (at maximum priority) and not
205          * during regular (lazy) initialisation so as to prevent overwriting the
206          * program's actual signal handler, if it has one.
207          *
208          * In theory this is not required for the Android logger backend; however,
209          * this early we don't yet know the backend and also it is good to behave
210          * consistently in this regard anyway.
211          *
212          * We don't revert this in a destructor because Unix signals are bonkers
213          * and we have no way to do this cleanly. Most libdlog users don't use
214          * runtime linking so this would mostly done at program exit either way. */
215         signal(SIGPIPE, SIG_IGN);
216 }
217
218 static bool first = true;
219 static bool initialize()
220 {
221         if (is_initialized)
222                 return true;
223
224         /* The mutex acts as a barrier, but otherwise the C language's
225          * machine abstraction is single-threaded. This means that the
226          * compiler is free to rearrange calls inside the mutex according
227          * to the as-if rule because it doesn't care if another thread can
228          * access it in parallel. In particular, `is_initialized = true`
229          * directly after `__configure()` could be rearranged to go in
230          * front of it because it is not touched inside that function
231          * if the compiler thinks it helps somehow (not unlikely: since
232          * it is checked before the mutex, it is very probable for it to
233          * still be in the CPU register or something like that). On top
234          * of that, some architectures (in particular, armv7l) don't have
235          * strict memory guarantees and can reorder actual memory stores
236          * on their own, even if the compiler didn't do anything fancy
237          * when creating machine code. For more info about the issue,
238          * see https://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
239          *
240          * Ultimately this means that there needs to be some sort of
241          * barrier between `__configure` and `is_initialized = true`,
242          * and the simplest way to achieve that is to just wait until
243          * the second entry into the mutex. */
244
245         bool ret;
246         pthread_mutex_lock(&log_construction_lock);
247                 if (first)
248                         first = !__configure();
249                 else
250                         is_initialized = true;
251                 ret = !first;
252         pthread_mutex_unlock(&log_construction_lock);
253         return ret;
254 }
255
256 /**
257  * @brief Fatal assertion
258  * @details Conditionally crash the sucka who sent the log
259  * @param[in] prio Priority of the log
260  */
261 static void __dlog_fatal_assert(int prio)
262 {
263         assert(!fatal_assert || (prio != DLOG_FATAL));
264 }
265
266 /**
267  * @brief Check log validity
268  * @details Checks whether the log is valid and eligible for printing
269  * @param[in] log_id The target buffer ID
270  * @param[in] prio The log's priority
271  * @param[in] tag The log's tag
272  * @return DLOG_ERROR_NONE on success, else an error code.
273  * @retval DLOG_ERROR_INVALID_PARAMETER Invalid parameter
274  */
275 static int dlog_check_validity(log_id_t log_id, int prio, const char *tag)
276 {
277         (void) prio;
278         if (!tag)
279                 return DLOG_ERROR_INVALID_PARAMETER;
280
281         if (log_id <= LOG_ID_INVALID || LOG_ID_MAX <= log_id)
282                 return DLOG_ERROR_INVALID_PARAMETER;
283
284         return DLOG_ERROR_NONE;
285 }
286
287 /**
288  * @brief Check log against limiter rules
289  * @details Checks whether the log passes current limiter rules
290  * @param[in] log_id The target buffer ID
291  * @param[in] prio The log's priority
292  * @param[in] tag The log's tag
293  * @return DLOG_ERROR_NONE on success, else an error code.
294  * @retval DLOG_ERROR_NOT_PERMITTED Not permitted
295  */
296 static int dlog_check_limiter(log_id_t log_id, int prio, const char *tag)
297 {
298         if (!debugmode && prio <= DLOG_DEBUG)
299                 return DLOG_ERROR_NOT_PERMITTED;
300
301         if (dynamic_config)
302                 __dynamic_config_update();
303
304         if (limiter) {
305                 int should_log = 0;
306                 if (!pthread_rwlock_rdlock(&log_limiter_lock)) {
307                         should_log = __log_limiter_pass_log(tag, prio);
308                         pthread_rwlock_unlock(&log_limiter_lock);
309                 }
310
311                 if (!should_log) {
312                         return DLOG_ERROR_NOT_PERMITTED;
313                 } else if (should_log < 0) {
314                         struct timespec tp;
315                         int result = clock_gettime(CLOCK_MONOTONIC, &tp);
316                         if (result < 0)
317                                 return DLOG_ERROR_NOT_PERMITTED;
318                         write_to_log(log_id, prio, tag,
319                                         "Your log has been blocked due to limit of log lines per minute.", &tp);
320                         return DLOG_ERROR_NOT_PERMITTED;
321                 }
322         }
323
324         /* This can change due to __dynamic_config_update(), but is atomic and its
325          * value implies nothing else so does not need to be under a lock. */
326         if (!plog[log_id])
327                 return DLOG_ERROR_NOT_PERMITTED;
328
329         return DLOG_ERROR_NONE;
330 }
331
332 static int __write_to_log_critical_section(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap, bool check_should_log)
333 {
334         if (check_should_log && prio < priority_filter_level)
335                 return DLOG_ERROR_NONE;
336
337         if ((check_should_log || limiter_apply_to_all_buffers) && (dlog_check_limiter(log_id, prio, tag) < 0))
338                 return DLOG_ERROR_NONE;
339
340         char buf[LOG_MAX_PAYLOAD_SIZE];
341         size_t len = vsnprintf(buf, sizeof buf, fmt, ap);
342         if (len < 0)
343                 return DLOG_ERROR_NONE;
344         else if (len >= sizeof buf)
345                 len = sizeof buf - 1;
346
347         struct timespec tp;
348         int result = clock_gettime(CLOCK_MONOTONIC, &tp);
349         if (result < 0)
350                 return DLOG_ERROR_NONE;
351
352         if (deduplicate(buf, len, &tp))
353                 return 0;
354         return write_to_log(log_id, prio, tag, buf, &tp);
355 }
356
357 static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap, bool check_should_log, bool secure_log)
358 {
359         int ret = dlog_check_validity(log_id, prio, tag);
360         if (ret < 0)
361                 return ret;
362
363         /* Threads can be cancelled before they give up a lock.
364          * Therefore cancellation is temporarily disabled.
365          * This solution is comparatively simple and cheap.
366          * The other solutions (cleanup handlers, robust mutexes)
367          * would be much more complicated and also inflict larger
368          * runtime costs. The downside of disabling cancellation
369          * is not a problem in our case because it is temporary
370          * and very brief so we don't keep an obsolete thread
371          * for much longer than we otherwise would. */
372         int old_cancel_state;
373         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancel_state);
374
375         /* The only thing that needs to be protected here is `write_to_log` since
376          * all other resources already have their own specific locks (and even the
377          * pointer could be made to point at a null handler instead of a true NULL)
378          * but giving this guarantee makes everything a lot simpler as it removes
379          * the risk of something suddenly becoming NULL during processing. */
380         if (!initialize() || !write_to_log)
381                 ret = DLOG_ERROR_NOT_PERMITTED;
382         else if (secure_log && !enable_secure_logs)
383                 ret = 0;
384         else
385                 ret = __write_to_log_critical_section(log_id, prio, tag, fmt, ap, check_should_log);
386
387         pthread_setcancelstate(old_cancel_state, NULL);
388
389         return ret;
390 }
391
392 int __critical_log_append_timestamp(char *buf, size_t buflen)
393 {
394         /* NB: the timestamp may slightly differ from the one that gets
395          * added onto the copy that goes into the regular buffer, and
396          * timestamp acquisition is duplicated. This would ideally be
397          * solved, but timestamps are currently added fairly deep in
398          * backend-specific functions so for now this will have to do.
399          * Also, since we're the sender, there is just this one set of
400          * timestamps, i.e. the send timestamp! The usual alternative
401          * set of receive timestamps will never have the opportunity
402          * to get added to the entry since this log is supposed to end
403          * up straight in the file (there's potentially the trusted
404          * writer binary but we're trying to keep the set of actions
405          * it needs to do to the minimum and those timestamps would
406          * in practice be the same anyway). */
407
408         struct timespec ts;
409         clock_gettime(CLOCK_REALTIME, &ts);
410         const time_t tt = ts.tv_sec;
411         const long int real_millisec = ts.tv_nsec / 1000000;
412         clock_gettime(CLOCK_MONOTONIC, &ts);
413         struct tm tmBuf;
414         struct tm *const ptm = localtime_r(&tt, &tmBuf);
415         assert(ptm); // we're in a short lived fork so asserts are fine and make things simple
416
417         int len = strftime(buf, buflen, "%m-%d %H:%M:%S", ptm);
418         assert(len != 0);
419
420         int tmp_len = snprintf(buf + len, buflen - len, ".%03ld", real_millisec);
421         assert(tmp_len > 0);
422         assert(tmp_len < buflen - len);
423         len += tmp_len;
424
425         tmp_len = strftime(buf + len, buflen - len, "%z ", ptm);
426         assert(tmp_len != 0);
427         len += tmp_len;
428
429         tmp_len = snprintf(buf + len, buflen - len, "%5lu.%03ld", ts.tv_sec, ts.tv_nsec / 1000000);
430         assert(tmp_len > 0);
431         assert(tmp_len < buflen - len);
432         len += tmp_len;
433
434         return len;
435 }
436
437 int __critical_log_build_msg(char *buf, size_t buflen, pid_t main_pid, pid_t main_tid, log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
438 {
439         int len = __critical_log_append_timestamp(buf, buflen);
440         const int metadata_len = snprintf(buf + len, buflen - len, " P%5d T%5d B%-6s %c/%-8s: ",
441                 main_pid,
442                 main_tid,
443                 log_name_by_id(log_id),
444                 filter_pri_to_char(prio),
445                 tag ?: "CRITICAL_NO_TAG");
446         assert(metadata_len > 0);
447         if (metadata_len >= buflen - len)
448                 return buflen - 1; // can genuinely happen with an exceedingly large tag
449         len += metadata_len;
450
451         const int content_len = vsnprintf(buf + len, buflen - len, fmt, ap);
452         assert(content_len >= 0); // 0 is legit with format == ""
453         if (content_len >= buflen - len)
454                 return buflen - 1;
455         len += content_len;
456
457         return len;
458 }
459
460 #ifndef UNIT_TEST
461 __attribute__ ((noreturn))
462 #endif
463 void __critical_log_child(pid_t main_pid, pid_t main_tid, log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
464 {
465         char buf[LOG_MAX_PAYLOAD_SIZE + 128]; // extra space for some metadata
466         const int len = __critical_log_build_msg(buf, sizeof buf - 1, main_pid, main_tid, log_id, prio, tag, fmt, ap);
467         buf[len] = '\n';
468         buf[len + 1] = '\0';
469
470         static const char *const path = "/usr/libexec/dlog-log-critical";
471         execl(path, path /* argv[0] convention */, buf, (char *) NULL);
472
473 #ifndef UNIT_TEST
474         /* Compilers are sometimes smart enough to recognize _exit's
475          * noreturn attribute, even if we wrap it with something that
476          * returns. This causes it to behave in unexpected ways, for
477          * example it can blow up the program regardless or it can
478          * optimize some conditionals out (and incorrectly enter them
479          * after the exit call fails to actually exit). This makes it
480          * unsuitable for tests. */
481
482         _exit(1); // not the regular `exit` so as not to trigger any `atexit` handlers prematurely
483 #endif
484 }
485
486 #ifndef UNIT_TEST // contains forks and exits, these don't work well with wrapping (see above)
487 void __critical_log(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
488 {
489         /* Critical log functionality is mostly done in a separate binary
490          * to handle security correctly (else every process would have to
491          * possess the necessary privilege to write onto that file, which
492          * would be opening a fairly nasty can of worms from the security
493          * point of view). Our use of exec() is why a simple thread would
494          * not suffice and we're resorting to a fork.
495          *
496          * The double fork, much like a double barreled 12 gauge shotgun,
497          * is an elegant solution designed to stop a zombie army. We'd be
498          * creating zombie processes if we didn't wait() for the children
499          * we spawn, but we don't really want to do that since it results
500          * in a needless delay. Instead, the writer process is actually a
501          * grandchild, with our direct child exiting immediately just for
502          * us to have something to wait on that is guaranteed not to take
503          * too long. The orphaned grandchild is adopted by init, who will
504          * take care to reap it when it dies. In addition to avoiding the
505          * delay, the client will not have any unexpected children (which
506          * could ruin logic in its own waits).
507          *
508          * Right after forks:
509          * ┌───────┐   ┌─────────┐   ┌─────────────┐   ┌────────┐
510          * │ pid 1 ├──>│ libdlog ├──>│ immediately ├──>│ execs  │
511          * │ init  │   │ client  │   │    exits    │   │ writer │
512          * └───────┘   └─────────┘   └─────────────┘   └────────┘
513          *
514          * Afterwards, libdlog has no children:
515          * ┌───────┐   ┌─────────┐                     ┌────────┐
516          * │ pid 1 ├──>│ libdlog │          ┌─────────>│ writer │
517          * │ init  ├─┐ │ client  │          │          │ binary │
518          * └───────┘ │ └─────────┘          │          └────────┘
519          *           └──────────────────────┘
520          */
521
522         const pid_t main_pid = getpid();
523         const pid_t main_tid = gettid();
524
525         const int temporary_exiter_pid = fork();
526         if (temporary_exiter_pid < 0)
527                 return;
528         if (temporary_exiter_pid != 0) {
529                 waitpid(temporary_exiter_pid, NULL, 0);
530                 return;
531         }
532
533         const int child_pid = fork();
534         if (child_pid < 0)
535                 _exit(1);
536         if (child_pid != 0)
537                 _exit(0);
538
539         __critical_log_child(main_pid, main_tid, log_id, prio, tag, fmt, ap);
540 }
541
542 int __dlog_critical_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
543 {
544         va_list ap;
545
546         va_start(ap, fmt);
547         __critical_log(log_id, prio, tag, fmt, ap);
548         va_end(ap);
549
550         va_start(ap, fmt);
551         int ret = __dlog_vprint(log_id, prio, tag, fmt, ap);
552         va_end(ap);
553
554         return ret;
555 }
556 #endif
557
558 int dlog_set_minimum_priority(int priority)
559 {
560         if (priority < DLOG_DEFAULT || priority > DLOG_PRIO_MAX)
561                 return DLOG_ERROR_INVALID_PARAMETER;
562
563         priority_filter_level = priority;
564         return DLOG_ERROR_NONE;
565 }
566
567 /**
568  * @brief Print log
569  * @details Print a log line
570  * @param[in] log_id The target buffer ID
571  * @param[in] prio Priority
572  * @param[in] tag tag
573  * @param[in] fmt Format (same as printf)
574  * @param[in] ap Argument list
575  * @return Bytes written, or negative error
576  */
577 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
578 {
579         int ret = __write_to_log(log_id, prio, tag, fmt, ap, true, false);
580         __dlog_fatal_assert(prio);
581
582         return ret;
583 }
584
585 /**
586  * @brief Print log
587  * @details Print a log line
588  * @param[in] log_id The target buffer ID
589  * @param[in] prio Priority
590  * @param[in] tag tag
591  * @param[in] fmt Format (same as printf)
592  * @return Bytes written, or negative error
593  */
594 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
595 {
596         va_list ap;
597
598         va_start(ap, fmt);
599         int ret = __dlog_vprint(log_id, prio, tag, fmt, ap);
600         va_end(ap);
601
602         return ret;
603 }
604
605 /**
606  * @brief Print log
607  * @details Print a log line
608  * @param[in] log_id The target buffer ID
609  * @param[in] prio Priority
610  * @param[in] tag tag
611  * @param[in] fmt Format (same as printf)
612  * @return Bytes written, or negative error
613  */
614 int __dlog_sec_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
615 {
616         if (!enable_secure_logs)
617                 return 0;
618
619         va_list ap;
620
621         va_start(ap, fmt);
622         int ret = __write_to_log(log_id, prio, tag, fmt, ap, true, true);
623         __dlog_fatal_assert(prio);
624         va_end(ap);
625
626         return ret;
627 }
628
629 int dlog_vprint(log_priority prio, const char *tag, const char *fmt, va_list ap)
630 {
631         return __write_to_log(LOG_ID_APPS, prio, tag, fmt, ap, false, false);
632 }
633
634 int dlog_print(log_priority prio, const char *tag, const char *fmt, ...)
635 {
636         va_list ap;
637
638         va_start(ap, fmt);
639         int ret = dlog_vprint(prio, tag, fmt, ap);
640         va_end(ap);
641
642         return ret;
643 }
644
645 /**
646  * @brief Finalize DLog
647  * @details Finalizes and deallocates the library
648  * @notes Used directly in tests; brings back the pre-init state
649  */
650 void __dlog_fini(void)
651 {
652         if (destroy_backend) {
653                 destroy_backend();
654                 destroy_backend = NULL;
655         }
656         write_to_log = NULL;
657         is_initialized = false;
658         first = true;
659
660         enable_secure_logs = true;
661         __deduplicate_destroy();
662         __log_limiter_destroy();
663         __dynamic_config_destroy();
664 }