summaryrefslogtreecommitdiff
path: root/tools/gator/daemon/Logging.cpp
blob: a800c4be2320631dabe7ed82feae25b796030f7a (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
79
80
81
/**
 * Copyright (C) ARM Limited 2010-2015. 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 "Logging.h"

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

// Global thread-safe logging
Logging logg;

Logging::Logging() : mDebug(true) {
	pthread_mutex_init(&mLoggingMutex, NULL);

	strcpy(mErrBuf, "Unknown Error");
}

Logging::~Logging() {
}

static void format(char *const buf, const size_t bufSize, const bool verbose, const char *const level, const char *const function, const char *const file, const int line, const char *const fmt, va_list args) {
	int len;

	if (verbose) {
		len = snprintf(buf, bufSize, "%s: %s(%s:%i): ", level, function, file, line);
	} else {
		buf[0] = 0;
		len = 0;
	}

	vsnprintf(buf + len, bufSize - 1 - len, fmt, args); //  subtract 1 for \0
}

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

	pthread_mutex_lock(&mLoggingMutex);
	va_start(args, fmt);
	format(mErrBuf, sizeof(mErrBuf), mDebug, "ERROR", function, file, line, fmt, args);
	va_end(args);
	pthread_mutex_unlock(&mLoggingMutex);

	fprintf(stderr, "%s\n", mErrBuf);
}

void Logging::_logSetup(const char *function, const char *file, int line, const char *fmt, ...) {
	char logBuf[4096]; // Arbitrarily large buffer to hold a string
	va_list args;

	va_start(args, fmt);
	format(logBuf, sizeof(logBuf), mDebug, "SETUP", function, file, line, fmt, args);
	va_end(args);

	pthread_mutex_lock(&mLoggingMutex);
	mSetup.append("%s|", logBuf);
	pthread_mutex_unlock(&mLoggingMutex);

	if (mDebug) {
		fprintf(stderr, "%s\n", logBuf);
	}
}

void Logging::_logMessage(const char *function, const char *file, int line, const char *fmt, ...) {
	if (mDebug) {
		char logBuf[4096]; // Arbitrarily large buffer to hold a string
		va_list args;

		va_start(args, fmt);
		format(logBuf, sizeof(logBuf), mDebug, "INFO", function, file, line, fmt, args);
		va_end(args);

		fprintf(stderr, "%s\n", logBuf);
	}
}