blob: 48d498147cd12723376d8f5840101aa28ad242c2 [file] [log] [blame]
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02001/*
2 * Copyright (C) 2018-2019 sysmocom - s.f.m.c. GmbH
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: AGPL-3.0+
6 *
7 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See the COPYING file in the main directory for details.
22 */
23
Pau Espin Pedrolc9202ab2019-07-01 20:13:32 +020024#include <pthread.h>
25
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010026#include <osmocom/core/logging.h>
27#include <osmocom/core/utils.h>
28#include "debug.h"
29
30/* default categories */
31static const struct log_info_cat default_categories[] = {
32 [DMAIN] = {
33 .name = "DMAIN",
34 .description = "Main generic category",
35 .color = NULL,
36 .enabled = 1, .loglevel = LOGL_NOTICE,
37 },
Pau Espin Pedrol441d82a2018-12-04 16:37:24 +010038 [DTRXCTRL] = {
39 .name = "DTRXCTRL",
40 .description = "TRX CTRL interface",
41 .color = "\033[1;33m",
42 .enabled = 1, .loglevel = LOGL_NOTICE,
43 },
Harald Welte5cc88582018-08-17 19:55:38 +020044 [DDEV] = {
45 .name = "DDEV",
46 .description = "Device/Driver specific code",
47 .color = NULL,
48 .enabled = 1, .loglevel = LOGL_INFO,
49 },
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020050 [DLMS] = {
51 .name = "DLMS",
Harald Welte5cc88582018-08-17 19:55:38 +020052 .description = "Logging from within LimeSuite itself",
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020053 .color = NULL,
54 .enabled = 1, .loglevel = LOGL_NOTICE,
55 },
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010056};
57
58const struct log_info log_info = {
59 .cat = default_categories,
60 .num_cat = ARRAY_SIZE(default_categories),
61};
Pau Espin Pedrolc9202ab2019-07-01 20:13:32 +020062
63pthread_mutex_t log_mutex;
64
65bool log_mutex_init() {
66 int rc;
67 pthread_mutexattr_t attr;
68
69 if ((rc = pthread_mutexattr_init(&attr))) {
70 fprintf(stderr, "pthread_mutexattr_init() failed: %d\n", rc);
71 return false;
72 }
73 if ((rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))) {
74 fprintf(stderr, "pthread_mutexattr_settype() failed: %d\n", rc);
75 return false;
76 }
77 if ((rc = pthread_mutex_init(&log_mutex, &attr))) {
78 fprintf(stderr, "pthread_mutex_init() failed: %d\n", rc);
79 return false;
80 }
81 if ((rc = pthread_mutexattr_destroy(&attr))) {
82 fprintf(stderr, "pthread_mutexattr_destroy() failed: %d\n", rc);
83 return false;
84 }
85 return true;
86 /* FIXME: do we need to call pthread_mutex_destroy() during process exit? */
87}
88
89/* If called inside a C++ destructor, use log_mutex_(un)lock_canceldisable() APIs instead.
90 See osmo-trx commit 86be40b4eb762d5c12e8e3f7388ca9f254e77b36 for more information */
91void log_mutex_lock() {
92 OSMO_ASSERT(!pthread_mutex_lock(&log_mutex));
93}
94
95void log_mutex_unlock() {
96 OSMO_ASSERT(!pthread_mutex_unlock(&log_mutex));
97}
98
99void log_mutex_lock_canceldisable(int *st) {
100 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, st);
101 log_mutex_lock();
102}
103
104void log_mutex_unlock_canceldisable(int st) {
105 log_mutex_unlock();
106 pthread_setcancelstate(st, NULL);
107}