aboutsummaryrefslogtreecommitdiff
path: root/daemon/Logging.cpp
blob: 5fd45b54f90ad8371c9008ca4f4c52b29f899fcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
 * Copyright (C) ARM Limited 2010-2013. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#ifdef WIN32
#define MUTEX_INIT()	mLoggingMutex = CreateMutex(NULL, false, NULL);
#define MUTEX_LOCK()	WaitForSingleObject(mLoggingMutex, 0xFFFFFFFF);
#define MUTEX_UNLOCK()	ReleaseMutex(mLoggingMutex);
#define snprintf		_snprintf
#else
#include <pthread.h>
#define MUTEX_INIT()	pthread_mutex_init(&mLoggingMutex, NULL)
#define MUTEX_LOCK()	pthread_mutex_lock(&mLoggingMutex)
#define MUTEX_UNLOCK()	pthread_mutex_unlock(&mLoggingMutex)
#endif

#include "Logging.h"

// Global thread-safe logging
Logging* logg = NULL;

Logging::Logging(bool debug) {
	mDebug = debug;
	MUTEX_INIT();

	strcpy(mErrBuf, "Unknown Error");
	strcpy(mLogBuf, "Unknown Message");
}

Logging::~Logging() {
}

void Logging::logError(const char* file, int line, const char* fmt, ...) {
	va_list	args;

	MUTEX_LOCK();
	if (mDebug) {
		snprintf(mErrBuf, sizeof(mErrBuf), "ERROR[%s:%d]: ", file, line);
	} else {
		mErrBuf[0] = 0;
	}

	va_start(args, fmt);
	vsnprintf(mErrBuf + strlen(mErrBuf), sizeof(mErrBuf) - 2 - strlen(mErrBuf), fmt, args); //  subtract 2 for \n and \0
	va_end(args);

	if (strlen(mErrBuf) > 0) {
		strcat(mErrBuf, "\n");
	}
	MUTEX_UNLOCK();
}

void Logging::logMessage(const char* fmt, ...) {
	if (mDebug) {
		va_list	args;

		MUTEX_LOCK();
		strcpy(mLogBuf, "INFO: ");

		va_start(args, fmt);
		vsnprintf(mLogBuf + strlen(mLogBuf), sizeof(mLogBuf) - 2 - strlen(mLogBuf), fmt, args); //  subtract 2 for \n and \0
		va_end(args);
		strcat(mLogBuf, "\n");

		fprintf(stdout, "%s", mLogBuf);
		fflush(stdout);
		MUTEX_UNLOCK();
	}
}