Commit 87c7b01d authored by Vittorio Giovara's avatar Vittorio Giovara Committed by Tristan Matthews

de-cpp-fy logger

The functions provided can be expressed in plain C.
Document and reformat the API.

Refs #47561
Change-Id: Ie93e5fb9a0847a5d2c1ee7c4d0b6e1704bffcbe6
parent dd399350
......@@ -63,7 +63,6 @@ libsflphone_la_LDFLAGS = \
libsflphone_la_CFLAGS = \
@CCGNU2_CFLAGS@ \
@CCEXT2_CFLAGS@ \
@ZRTPCPP_CFLAGS@ \
$(PJSIP_CFLAGS) \
@CCRTP_CFLAGS@ \
......@@ -82,7 +81,7 @@ libsflphone_la_SOURCES = conference.cpp \
eventthread.cpp \
call.cpp \
account.cpp \
logger.cpp \
logger.c \
numbercleaner.cpp \
fileutils.cpp \
threadloop.cpp \
......
......@@ -109,7 +109,7 @@ void History::ensurePath()
// If directory creation failed
if (errno != EEXIST) {
DEBUG("Cannot create directory: %s", userdata.c_str());
Logger::strErr();
strErr();
return;
}
}
......
......@@ -28,17 +28,16 @@
* as that of the covered work.
*/
#include "logger.h"
#include <cstdarg>
#include <string>
#include <cstdio>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
namespace Logger {
#include "logger.h"
bool consoleLog = false;
bool debugMode = false;
static int consoleLog;
static int debugMode;
void log(const int level, const char* format, ...)
void logger(const int level, const char* format, ...)
{
if (!debugMode && level == LOG_DEBUG)
return;
......@@ -71,28 +70,25 @@ void log(const int level, const char* format, ...)
}
}
void setConsoleLog(bool c)
void setConsoleLog(int c)
{
consoleLog = c;
}
void setDebugMode(bool d)
void setDebugMode(int d)
{
debugMode = d;
}
bool getDebugMode()
int getDebugMode(void)
{
return debugMode;
}
void strErr(const char *msg)
void strErr(void)
{
#ifdef __GLIBC__
if (strlen(msg))
ERROR("%s: %m", msg);
else
ERROR("%m");
ERROR("%m");
#else
char buf[1000];
const char *errstr;
......@@ -109,10 +105,6 @@ void strErr(const char *msg)
break;
}
if (strlen(msg))
ERROR("%s: %s", msg, errstr);
else
ERROR("%s", errstr);
ERROR("%s", errstr);
#endif
}
}
......@@ -28,32 +28,50 @@
* as that of the covered work.
*/
#ifndef LOGGER_H_
#define LOGGER_H_
#ifndef H_LOGGER
#define H_LOGGER
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <pthread.h>
#ifdef __ANDROID__
#include <cstring>
#include <android/log.h>
#else
#include <syslog.h>
#endif
#include <cstring>
/**
* Print something, coloring it depending on the level
*/
void logger(const int level, const char *format, ...);
/**
* Allow writing on the console
*/
void setConsoleLog(int c);
/**
* When debug mode is not set, logging will not print anything
*/
void setDebugMode(int d);
namespace Logger {
void log(const int, const char*, ...);
/**
* Return the current mode
*/
int getDebugMode(void);
void setConsoleLog(bool);
void setDebugMode(bool);
bool getDebugMode();
/**
* Thread-safe function to print the stringified contents of errno appended to a
* user defined message */
void strErr(const char *message = "");
};
* Thread-safe function to print the stringified contents of errno
*/
void strErr();
#define LOG_FORMAT(M, ...) "%s:%d:0x%x: " M, FILE_NAME, __LINE__, (unsigned long) pthread_self() & 0xffff, ##__VA_ARGS__
#define LOG_FORMAT(M, ...) "%s:%d:0x%x: " M, FILE_NAME, __LINE__, \
(unsigned long) pthread_self() & 0xffff, \
##__VA_ARGS__
#ifndef __ANDROID__
......@@ -62,23 +80,24 @@ void strErr(const char *message = "");
#define WARN(M, ...) LOGGER(M, LOG_WARNING, ##__VA_ARGS__)
#define INFO(M, ...) LOGGER(M, LOG_INFO, ##__VA_ARGS__)
#define DEBUG(M, ...) LOGGER(M, LOG_DEBUG, ##__VA_ARGS__)
#define LOGGER(M, LEVEL, ...) Logger::log(LEVEL, LOG_FORMAT(M, ##__VA_ARGS__))
#define LOGGER(M, LEVEL, ...) logger(LEVEL, LOG_FORMAT(M, ##__VA_ARGS__))
#else /* ANDROID */
#else /* __ANDROID__ */
#ifndef APP_NAME
#define APP_NAME "libsflphone"
#endif
#endif /* APP_NAME */
// Avoid printing whole path on android
#define FILE_NAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define FILE_NAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 \
: __FILE__)
#define ERROR(M, ...) LOGGER(M, ANDROID_LOG_ERROR, ##__VA_ARGS__)
#define WARN(M, ...) LOGGER(M, ANDROID_LOG_WARN, ##__VA_ARGS__)
#define INFO(M, ...) LOGGER(M, ANDROID_LOG_INFO, ##__VA_ARGS__)
#define DEBUG(M, ...) LOGGER(M, ANDROID_LOG_DEBUG, ##__VA_ARGS__)
#define LOGGER(M, LEVEL, ...) __android_log_print(LEVEL, APP_NAME, LOG_FORMAT(M, ##__VA_ARGS__))
#endif /* ANDROID */
#endif /* __ANDROID__ */
#define BLACK "\033[22;30m"
#define RED "\033[22;31m"
......@@ -98,5 +117,8 @@ void strErr(const char *message = "");
#define WHITE "\033[01;37m"
#define END_COLOR "\033[0m"
#endif // LOGGER_H_
#ifdef __cplusplus
}
#endif
#endif // H_LOGGER
......@@ -116,8 +116,8 @@ namespace {
// We've always print the title/version, so we can just exit
quit = true;
} else {
Logger::setConsoleLog(consoleFlag);
Logger::setDebugMode(debugFlag);
setConsoleLog(consoleFlag);
setDebugMode(debugFlag);
}
return quit;
}
......
......@@ -118,7 +118,7 @@ void init_once()
av_lockmgr_register(avcodecManageMutex);
if (Logger::getDebugMode())
if (getDebugMode())
av_log_set_level(AV_LOG_VERBOSE);
/* list of codecs tested and confirmed to work */
......
......@@ -84,7 +84,7 @@ bool SHMSink::start()
fd_ = shm_open(shm_name_.c_str(), flags, perms);
if (fd_ < 0) {
ERROR("could not open shm area \"%s\"", shm_name_.c_str());
Logger::strErr();
strErr();
return false;
}
} else {
......@@ -94,7 +94,7 @@ bool SHMSink::start()
shm_name_ = name.str();
fd_ = shm_open(shm_name_.c_str(), flags, perms);
if (fd_ < 0 and errno != EEXIST) {
Logger::strErr();
strErr();
return false;
}
}
......@@ -107,7 +107,7 @@ bool SHMSink::start()
if (ftruncate(fd_, shm_area_len_)) {
ERROR("Could not make shm area large enough for header");
Logger::strErr();
strErr();
return false;
}
......@@ -133,7 +133,7 @@ bool SHMSink::start()
bool SHMSink::stop()
{
if (fd_ >= 0 and close(fd_) == -1)
Logger::strErr();
strErr();
fd_ = -1;
......@@ -159,13 +159,13 @@ bool SHMSink::resize_area(size_t desired_length)
if (munmap(shm_area_, shm_area_len_)) {
ERROR("Could not unmap shared area");
Logger::strErr();
strErr();
return false;
}
if (ftruncate(fd_, desired_length)) {
ERROR("Could not resize shared area");
Logger::strErr();
strErr();
return false;
}
......
......@@ -124,7 +124,7 @@ int udp_socket_create(sockaddr_storage *addr, socklen_t *addr_len,
// on local port
if (bind(udp_fd, reinterpret_cast<sockaddr*>(addr), *addr_len) < 0) {
ERROR("Bind failed");
Logger::strErr();
strErr();
close(udp_fd);
udp_fd = -1;
}
......@@ -168,9 +168,9 @@ void SocketPair::interrupt()
void SocketPair::closeSockets()
{
if (rtcpHandle_ > 0 and close(rtcpHandle_))
Logger::strErr();
strErr();
if (rtpHandle_ > 0 and close(rtpHandle_))
Logger::strErr();
strErr();
}
void SocketPair::openSockets(const char *uri, int local_rtp_port)
......
......@@ -326,7 +326,7 @@ bool VideoDeviceMonitorImpl::addDevice(const string &dev)
int fd = open(dev.c_str(), O_RDWR);
if (fd == -1) {
ERROR("Problem opening device");
Logger::strErr();
strErr();
return false;
}
......
......@@ -63,8 +63,8 @@ int main(int argc, char* argv[])
{
atexit(cleanup);
printf("\nSFLphone Daemon Test Suite, by Savoir-Faire Linux 2004-2014\n\n");
Logger::setConsoleLog(true);
Logger::setDebugMode(true);
setConsoleLog(true);
setDebugMode(true);
fileutils::FileHandle f(fileutils::create_pidfile());
if (f.fd == -1) {
fprintf(stderr, "An sflphoned instance is already running, quitting...\n");
......@@ -96,7 +96,7 @@ int main(int argc, char* argv[])
} else if (strcmp("--debug", argv[1]) == 0) {
argvIndex++;
Logger::setDebugMode(true);
setDebugMode(true);
INFO("Debug mode activated");
} else if (strcmp("--xml", argv[1]) == 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment