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