summaryrefslogtreecommitdiff
path: root/src/libcamera/log.cpp
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2018-12-04 23:52:34 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2018-12-06 16:43:04 +0200
commitedbd2059d8a4bd759302ada4368fa4055638fd7f (patch)
tree13736c5f0d888b60fbfe6f6a94d420a5bd26b81c /src/libcamera/log.cpp
parent913b3ee817a34bd78d5a8dc4fc9025920202db09 (diff)
libcamera: Add initial logger
The logger is based on the ostream API, allowing code to log messages in a native way. It automatically logs the time stamp, severity level, file name and line number. Many important features are missing, such as logging to file, logging classes, and log filtering based on the severity level, file name and class. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Diffstat (limited to 'src/libcamera/log.cpp')
-rw-r--r--src/libcamera/log.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp
new file mode 100644
index 00000000..18ccfa32
--- /dev/null
+++ b/src/libcamera/log.cpp
@@ -0,0 +1,81 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2018, Google Inc.
+ *
+ * log.h - Logging infrastructure
+ */
+
+#include <cstdio>
+#include <ctime>
+#include <iomanip>
+#include <string.h>
+
+#include "log.h"
+#include "utils.h"
+
+/**
+ * \file log.h
+ * \brief Logging infrastructure
+ */
+
+namespace libcamera {
+
+/**
+ * \enum LogSeverity
+ * Log message severity
+ * \var Info
+ * Informational message
+ * \var Warning
+ * Warning message, signals a potential issue
+ * \var Error
+ * Error message, signals an unrecoverable issue
+ */
+
+/**
+ * \def LOG(severity)
+ * \brief Log a message
+ *
+ * Return an std::ostream reference to which a message can be logged using the
+ * iostream API. The \a severity controls whether the message is printed or
+ * dropped, depending on the global log level.
+ */
+
+static const char *log_severity_name(LogSeverity severity)
+{
+ static const char * const names[] = {
+ "INFO",
+ "WARN",
+ " ERR",
+ };
+
+ if ((unsigned int)severity < ARRAY_SIZE(names))
+ return names[severity];
+ else
+ return "UNKN";
+}
+
+LogMessage::LogMessage(const char *fileName, unsigned int line,
+ LogSeverity severity)
+{
+ /* Log the timestamp, severity and file information. */
+ struct timespec timestamp;
+ clock_gettime(CLOCK_MONOTONIC, &timestamp);
+ msgStream << "[" << timestamp.tv_sec / (60 * 60) << ":"
+ << std::setw(2) << (timestamp.tv_sec / 60) % 60 << ":"
+ << std::setw(2) << timestamp.tv_sec % 60 << "."
+ << std::setw(9) << timestamp.tv_nsec << "]";
+
+ msgStream << " " << log_severity_name(severity);
+ msgStream << " " << basename(fileName) << ":" << line << " ";
+}
+
+LogMessage::~LogMessage()
+{
+ msgStream << std::endl;
+
+ std::string msg(msgStream.str());
+ fwrite(msg.data(), msg.size(), 1, stderr);
+ fflush(stderr);
+}
+
+};