summaryrefslogtreecommitdiff
path: root/qcom/qrtr/lib/logging.c
diff options
context:
space:
mode:
Diffstat (limited to 'qcom/qrtr/lib/logging.c')
-rw-r--r--qcom/qrtr/lib/logging.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/qcom/qrtr/lib/logging.c b/qcom/qrtr/lib/logging.c
new file mode 100644
index 0000000..0b0f103
--- /dev/null
+++ b/qcom/qrtr/lib/logging.c
@@ -0,0 +1,73 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <syslog.h>
+
+#define QLOG_BUF_SIZE 512
+
+static const char default_tag[] = "libqrtr";
+static const char *current_tag = default_tag;
+static int min_priority = LOG_INFO;
+
+static bool logging_to_syslog = false;
+
+void qlog_setup(const char *tag, bool use_syslog)
+{
+ current_tag = tag;
+ logging_to_syslog = use_syslog;
+
+ openlog(tag, LOG_PID, LOG_USER);
+}
+
+void qlog_set_min_priority(int priority)
+{
+ if (priority < LOG_EMERG || priority > LOG_DEBUG)
+ return;
+
+ min_priority = priority;
+}
+
+static const char *get_priority_string(int priority)
+{
+ switch (priority) {
+ case LOG_EMERG:
+ return "EMERG";
+ case LOG_ALERT:
+ return "ALERT";
+ case LOG_CRIT:
+ return "CRIT";
+ case LOG_ERR:
+ return "ERROR";
+ case LOG_WARNING:
+ return "WARNING";
+ case LOG_NOTICE:
+ return "NOTICE";
+ case LOG_INFO:
+ return "INFO";
+ case LOG_DEBUG:
+ return "DEBUG";
+ }
+ return "";
+}
+
+void qlog(int priority, const char *format, ...)
+{
+ va_list ap;
+
+ if (priority > min_priority)
+ return;
+
+ va_start(ap, format);
+
+ if (logging_to_syslog) {
+ vsyslog(priority, format, ap);
+ } else {
+ char buf[QLOG_BUF_SIZE];
+ vsnprintf(buf, QLOG_BUF_SIZE, format, ap);
+
+ fprintf(stderr, "%s %s: %s\n",
+ get_priority_string(priority), current_tag, buf);
+ }
+
+ va_end(ap);
+}