summaryrefslogtreecommitdiff
path: root/daemons/gptp/common/gptp_log.cpp
blob: a1a5809f757fb30bdad9475a07cc92387c80d8d7 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*************************************************************************************************************
Copyright (c) 2012-2016, Harman International Industries, Incorporated
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS LISTED "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS LISTED BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************************************************/

#include <gptp_log.hpp>

#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <platform.hpp>

// MS VC++ 2013 has C++11 but not C11 support, use this to get millisecond resolution
#include <chrono>

#ifdef GENIVI_DLT
DLT_DECLARE_CONTEXT(dlt_con_gptp);
#endif

void gptplogRegister(void)
{
#ifdef GENIVI_DLT
	DLT_REGISTER_APP("GPTP","OpenAVB gPTP");
	DLT_REGISTER_CONTEXT(dlt_con_gptp, "GNRL", "General Context");
#endif
}

void gptplogUnregister(void)
{
#ifdef GENIVI_DLT
	DLT_UNREGISTER_CONTEXT(dlt_con_gptp);
	DLT_UNREGISTER_APP();
#endif
}

void gptpLog(GPTP_LOG_LEVEL level, const char *tag, const char *path, int line, const char *fmt, ...)
{
	char msg[1024];

	va_list args;
	va_start(args, fmt);
	vsprintf(msg, fmt, args);

#ifndef GENIVI_DLT
	std::chrono::system_clock::time_point cNow = std::chrono::system_clock::now();
	time_t tNow = std::chrono::system_clock::to_time_t(cNow);
	struct tm tmNow;
	PLAT_localtime(&tNow, &tmNow);
	std::chrono::system_clock::duration roundNow = cNow - std::chrono::system_clock::from_time_t(tNow);
	long int millis = (long int) std::chrono::duration_cast<std::chrono::milliseconds>(roundNow).count();

	if (path) {
		fprintf(stderr, "%s: GPTP [%2.2d:%2.2d:%2.2d:%3.3ld] [%s:%u] %s\n",
			   tag, tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec, millis, path, line, msg);
	}
	else {
		fprintf(stderr, "%s: GPTP [%2.2d:%2.2d:%2.2d:%3.3ld] %s\n",
			   tag, tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec, millis, msg);
	}
#else
	DltLogLevelType dlt_level; 

	switch (level) {
	case GPTP_LOG_LVL_CRITICAL:
		dlt_level = DLT_LOG_FATAL;
		break;
	case GPTP_LOG_LVL_ERROR:
		dlt_level = DLT_LOG_ERROR;
		break;
	case GPTP_LOG_LVL_EXCEPTION:
	case GPTP_LOG_LVL_WARNING:
		dlt_level = DLT_LOG_WARN;
		break;
	case GPTP_LOG_LVL_INFO:
	case GPTP_LOG_LVL_STATUS:
		dlt_level = DLT_LOG_INFO;
		break;
	case GPTP_LOG_LVL_DEBUG:
		dlt_level = DLT_LOG_DEBUG;
		break;
	case GPTP_LOG_LVL_VERBOSE:
		dlt_level = DLT_LOG_VERBOSE;
		break;
	default:
		dlt_level = DLT_LOG_INFO;
		break;
	}

	DLT_LOG(dlt_con_gptp, dlt_level, DLT_STRING(msg));
#endif

}