1 /*
2 * Logging.cpp
3 *
4 * Copyright (C) 2007 - 2011 Texas Instruments Incorporated - http://www.ti.com/
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
17 * distribution.
18 *
19 * Neither the name of Texas Instruments Incorporated nor the names of
20 * its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <pch.h>
37
38 #include <chrono>
39 #include <iomanip>
40
41 #include "Logging.h"
42
43 #ifdef DB_PRINT
44
45 //#define USE_STDOUT
46
47 using namespace TI::DLL430::Logging;
48
DefaultLogger()49 Debug& TI::DLL430::Logging::DefaultLogger()
50 {
51 static Debug defaultLogger;
52 return defaultLogger;
53 }
54
Debug()55 Debug::Debug()
56 : filePtr(stdout)
57 {
58 #ifndef USE_STDOUT
59 filePtr = fopen("comm.log", "w");
60 #endif
61
62 if (!filePtr)
63 fprintf(stderr, "File could not be opened\n!");
64 }
65
~Debug()66 Debug::~Debug()
67 {
68 if (filePtr)
69 fclose(filePtr);
70 }
71
PrintSendBuffer(const uint8_t * buf,long size)72 void Debug::PrintSendBuffer(const uint8_t *buf, long size)
73 {
74 PrintBuffer(buf, size, "send:");
75 }
76
PrintReceiveBuffer(const uint8_t * buf,long size)77 void Debug::PrintReceiveBuffer(const uint8_t *buf, long size)
78 {
79 PrintBuffer(buf, size, "receive:");
80 }
81
PrintBuffer(const uint8_t * buf,long size,const char * direction)82 void Debug::PrintBuffer(const uint8_t *buf, long size, const char *direction)
83 {
84 if (filePtr)
85 {
86 const auto now = std::chrono::system_clock::now();
87 const auto timeNow = std::chrono::system_clock::to_time_t(now);
88 const auto lt = std::localtime(&timeNow);
89 const auto usSinceEpoch = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
90
91 static const char* monthNames[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
92
93 std::stringstream sstr;
94 sstr << std::setfill('0') << "\n"
95 << (1900 + lt->tm_year) << '-' << monthNames[lt->tm_mon % 12] << '-' << std::setw(2) << lt->tm_mday << ' '
96 << std::setw(2) << lt->tm_hour << ':' << std::setw(2) << lt->tm_min << ':' << std::setw(2) << lt->tm_sec << '.'
97 << std::setw(6) << (usSinceEpoch % 1000000) << " "
98 << direction << " " << std::this_thread::get_id()
99 << std::endl << "[" << std::hex;
100
101 for (long i = 0 ; i < size; ++i)
102 {
103 sstr << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(buf[i]);
104 if (i != size -1)
105 sstr << "][";
106 }
107 sstr << "]" << std::endl << std::dec;
108 fprintf(filePtr, "%s", sstr.str().c_str());
109 fflush(filePtr);
110 }
111 }
112
113 #endif
114