]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/libcharon/plugins/android_log/android_log_logger.c
48bcaa5772277c2c3380507af99dc48a72d49903
[people/ms/strongswan.git] / src / libcharon / plugins / android_log / android_log_logger.c
1 /*
2 * Copyright (C) 2010-2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <string.h>
17 #include <android/log.h>
18
19 #include "android_log_logger.h"
20
21 #include <library.h>
22 #include <daemon.h>
23 #include <threading/mutex.h>
24
25 typedef struct private_android_log_logger_t private_android_log_logger_t;
26
27 /**
28 * Private data of an android_log_logger_t object
29 */
30 struct private_android_log_logger_t {
31
32 /**
33 * Public interface
34 */
35 android_log_logger_t public;
36
37 /**
38 * logging level
39 */
40 int level;
41
42 /**
43 * Mutex to ensure multi-line log messages are not torn apart
44 */
45 mutex_t *mutex;
46 };
47
48 METHOD(logger_t, log_, void,
49 private_android_log_logger_t *this, debug_t group, level_t level,
50 int thread, ike_sa_t* ike_sa, const char *message)
51 {
52 int prio = level > 1 ? ANDROID_LOG_DEBUG : ANDROID_LOG_INFO;
53 char sgroup[16];
54 const char *current = message, *next;
55 snprintf(sgroup, sizeof(sgroup), "%N", debug_names, group);
56 this->mutex->lock(this->mutex);
57 while (TRUE)
58 { /* log each line separately */
59 next = strchr(current, '\n');
60 if (next == NULL)
61 {
62 __android_log_print(prio, "charon", "%.2d[%s] %s\n",
63 thread, sgroup, current);
64 break;
65 }
66 __android_log_print(prio, "charon", "%.2d[%s] %.*s\n",
67 thread, sgroup, (int)(next - current), current);
68 current = next + 1;
69 }
70 this->mutex->unlock(this->mutex);
71 }
72
73 METHOD(logger_t, get_level, level_t,
74 private_android_log_logger_t *this, debug_t group)
75 {
76 return this->level;
77 }
78
79 METHOD(android_log_logger_t, destroy, void,
80 private_android_log_logger_t *this)
81 {
82 this->mutex->destroy(this->mutex);
83 free(this);
84 }
85
86 /**
87 * Described in header.
88 */
89 android_log_logger_t *android_log_logger_create()
90 {
91 private_android_log_logger_t *this;
92
93 INIT(this,
94 .public = {
95 .logger = {
96 .log = _log_,
97 .get_level = _get_level,
98 },
99 .destroy = _destroy,
100 },
101 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
102 .level = lib->settings->get_int(lib->settings,
103 "%s.plugins.android_log.loglevel", 1, charon->name),
104 );
105
106 return &this->public;
107 }
108