blob: 8846622d950f0def1da4a45f6d6d357a6c807786 [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
Jon Medhurstb1d07442015-05-08 12:04:18 +01002 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
Jon Medhurstaaf37a32013-06-11 12:10:56 +01003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
Jon Medhurst15ce78d2014-04-10 09:02:02 +01009#include "Logging.h"
10
Jon Medhurstaaf37a32013-06-11 12:10:56 +010011#include <stdio.h>
12#include <stdlib.h>
13#include <stdarg.h>
14#include <string.h>
15
16#ifdef WIN32
Jon Medhurst96b56152014-10-30 18:01:15 +000017#define MUTEX_INIT() mLoggingMutex = CreateMutex(NULL, false, NULL);
18#define MUTEX_LOCK() WaitForSingleObject(mLoggingMutex, 0xFFFFFFFF);
19#define MUTEX_UNLOCK() ReleaseMutex(mLoggingMutex);
20#define snprintf _snprintf
Jon Medhurstaaf37a32013-06-11 12:10:56 +010021#else
22#include <pthread.h>
Jon Medhurst96b56152014-10-30 18:01:15 +000023#define MUTEX_INIT() pthread_mutex_init(&mLoggingMutex, NULL)
24#define MUTEX_LOCK() pthread_mutex_lock(&mLoggingMutex)
25#define MUTEX_UNLOCK() pthread_mutex_unlock(&mLoggingMutex)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010026#endif
27
Jon Medhurstaaf37a32013-06-11 12:10:56 +010028// Global thread-safe logging
29Logging* logg = NULL;
30
31Logging::Logging(bool debug) {
32 mDebug = debug;
33 MUTEX_INIT();
34
35 strcpy(mErrBuf, "Unknown Error");
36 strcpy(mLogBuf, "Unknown Message");
37}
38
39Logging::~Logging() {
40}
41
Jon Medhurstb1d07442015-05-08 12:04:18 +010042void Logging::_logError(const char *function, const char *file, int line, const char *fmt, ...) {
Jon Medhurst96b56152014-10-30 18:01:15 +000043 va_list args;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010044
45 MUTEX_LOCK();
46 if (mDebug) {
Jon Medhurstb1d07442015-05-08 12:04:18 +010047 snprintf(mErrBuf, sizeof(mErrBuf), "ERROR: %s(%s:%i): ", function, file, line);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010048 } else {
49 mErrBuf[0] = 0;
50 }
51
52 va_start(args, fmt);
53 vsnprintf(mErrBuf + strlen(mErrBuf), sizeof(mErrBuf) - 2 - strlen(mErrBuf), fmt, args); // subtract 2 for \n and \0
54 va_end(args);
55
56 if (strlen(mErrBuf) > 0) {
57 strcat(mErrBuf, "\n");
58 }
59 MUTEX_UNLOCK();
60}
61
Jon Medhurstb1d07442015-05-08 12:04:18 +010062void Logging::_logMessage(const char *function, const char *file, int line, const char *fmt, ...) {
Jon Medhurstaaf37a32013-06-11 12:10:56 +010063 if (mDebug) {
Jon Medhurst96b56152014-10-30 18:01:15 +000064 va_list args;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010065
66 MUTEX_LOCK();
Jon Medhurstb1d07442015-05-08 12:04:18 +010067 snprintf(mLogBuf, sizeof(mLogBuf), "INFO: %s(%s:%i): ", function, file, line);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010068
69 va_start(args, fmt);
70 vsnprintf(mLogBuf + strlen(mLogBuf), sizeof(mLogBuf) - 2 - strlen(mLogBuf), fmt, args); // subtract 2 for \n and \0
71 va_end(args);
72 strcat(mLogBuf, "\n");
73
74 fprintf(stdout, "%s", mLogBuf);
75 fflush(stdout);
76 MUTEX_UNLOCK();
77 }
78}