2 * Copyright (C) 2010-2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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>.
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
17 #include <android/log.h>
19 #include "android_log_logger.h"
23 #include <threading/mutex.h>
25 typedef struct private_android_log_logger_t private_android_log_logger_t
;
28 * Private data of an android_log_logger_t object
30 struct private_android_log_logger_t
{
35 android_log_logger_t
public;
43 * Mutex to ensure multi-line log messages are not torn apart
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
)
52 int prio
= level
> 1 ? ANDROID_LOG_DEBUG
: ANDROID_LOG_INFO
;
54 const char *current
= message
, *next
;
55 snprintf(sgroup
, sizeof(sgroup
), "%N", debug_names
, group
);
56 this->mutex
->lock(this->mutex
);
58 { /* log each line separately */
59 next
= strchr(current
, '\n');
62 __android_log_print(prio
, "charon", "%.2d[%s] %s\n",
63 thread
, sgroup
, current
);
66 __android_log_print(prio
, "charon", "%.2d[%s] %.*s\n",
67 thread
, sgroup
, (int)(next
- current
), current
);
70 this->mutex
->unlock(this->mutex
);
73 METHOD(logger_t
, get_level
, level_t
,
74 private_android_log_logger_t
*this, debug_t group
)
79 METHOD(android_log_logger_t
, destroy
, void,
80 private_android_log_logger_t
*this)
82 this->mutex
->destroy(this->mutex
);
87 * Described in header.
89 android_log_logger_t
*android_log_logger_create()
91 private_android_log_logger_t
*this;
97 .get_level
= _get_level
,
101 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
102 .level
= lib
->settings
->get_int(lib
->settings
,
103 "%s.plugins.android_log.loglevel", 1, charon
->name
),
106 return &this->public;