Skip to content

Commit 8a7393b

Browse files
committed
(internal) Improved log output code
1 parent ede2860 commit 8a7393b

File tree

7 files changed

+181
-64
lines changed

7 files changed

+181
-64
lines changed

src/client/WioCellularTcpClient2.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "internal/CountdownTimer.hpp"
1212
#include "internal/Misc.hpp"
1313
#include "internal/UsedConnectIds.hpp"
14+
#include "internal/WioLog.hpp"
1415
#include "WioCellularResult.hpp"
1516

1617
namespace wiocellular::client
@@ -73,7 +74,7 @@ namespace wiocellular::client
7374
private:
7475
void setState(State state)
7576
{
76-
printf("---> WioCellularTcpClient2<%d> state changed %d to %d\n", ConnectId_, static_cast<int>(State_), static_cast<int>(state));
77+
wiocellular::internal::WioLog(wiocellular::internal::WioLogType::INFO, "WioCellularTcpClient2<%d> state changed %d to %d", ConnectId_, static_cast<int>(State_), static_cast<int>(state));
7778
State_ = state;
7879
}
7980

@@ -133,7 +134,7 @@ namespace wiocellular::client
133134
const std::string urcQiurcRecvPrefix = wiocellular::internal::stringFormat("+QIURC: \"recv\",%d,", ConnectId_);
134135
if (response == urcQiurcRecv || response.starts_with(urcQiurcRecvPrefix))
135136
{
136-
printf("---> WioCellularTcpClient2<%d> received\n", ConnectId_);
137+
wiocellular::internal::WioLog(wiocellular::internal::WioLogType::INFO, "WioCellularTcpClient2<%d> received", ConnectId_);
137138
ReceivedNofity_ = true;
138139
return true;
139140
}
@@ -290,7 +291,7 @@ namespace wiocellular::client
290291
if (getState() == State::Opened && millis() - openedTime_ < 150000)
291292
{
292293
const auto wait = 150000 - (millis() - openedTime_);
293-
printf("---> WioCellularTcpClient2<%d> forced to wait %lu\n", ConnectId_, wait);
294+
wiocellular::internal::WioLog(wiocellular::internal::WioLogType::INFO, "WioCellularTcpClient2<%d> forced to wait %lu", ConnectId_, wait);
294295
WioCellular.doWork(wait, [this]
295296
{ return getState() != State::Opened; });
296297
}

src/client/WioCellularUdpClient2.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "internal/CountdownTimer.hpp"
1212
#include "internal/Misc.hpp"
1313
#include "internal/UsedConnectIds.hpp"
14+
#include "internal/WioLog.hpp"
1415
#include "WioCellularResult.hpp"
1516

1617
namespace wiocellular::client
@@ -68,7 +69,7 @@ namespace wiocellular::client
6869
private:
6970
void setState(State state)
7071
{
71-
printf("---> WioCellularUdpClient2<%d> state changed %d to %d\n", ConnectId_, static_cast<int>(State_), static_cast<int>(state));
72+
wiocellular::internal::WioLog(wiocellular::internal::WioLogType::INFO, "WioCellularUdpClient2<%d> state changed %d to %d", ConnectId_, static_cast<int>(State_), static_cast<int>(state));
7273
State_ = state;
7374
}
7475

@@ -119,7 +120,7 @@ namespace wiocellular::client
119120
const std::string urcQiurcRecvPrefix = wiocellular::internal::stringFormat("+QIURC: \"recv\",%d,", ConnectId_);
120121
if (response == urcQiurcRecv || response.starts_with(urcQiurcRecvPrefix))
121122
{
122-
printf("---> WioCellularUdpClient2<%d> received\n", ConnectId_);
123+
wiocellular::internal::WioLog(wiocellular::internal::WioLogType::INFO, "---> WioCellularUdpClient2<%d> received", ConnectId_);
123124
ReceivedNofity_ = true;
124125
return true;
125126
}
@@ -281,7 +282,7 @@ namespace wiocellular::client
281282
if (getState() == State::Opened && millis() - openedTime_ < 150000)
282283
{
283284
const auto wait = 150000 - (millis() - openedTime_);
284-
printf("---> WioCellularUdpClient2<%d> forced to wait %lu\n", ConnectId_, wait);
285+
wiocellular::internal::WioLog(wiocellular::internal::WioLogType::INFO, "WioCellularUdpClient2<%d> forced to wait %lu", ConnectId_, wait);
285286
WioCellular.doWork(wait, [this]
286287
{ return getState() != State::Opened; });
287288
}

src/component/nonvolatilememory/Adafruit_SPIFlashMemory.hpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,6 @@ namespace wiocellular
7979
if (Flash_.readBuffer(address, reinterpret_cast<uint8_t *>(data), dataSize) != dataSize)
8080
return -1;
8181

82-
// // for debug
83-
// Serial.printf("R [%04X] ", address);
84-
// for (size_t i = 0; i < dataSize; ++i)
85-
// {
86-
// Serial.printf("%02X ", reinterpret_cast<uint8_t *>(data)[i]);
87-
// }
88-
// Serial.println();
89-
9082
return 0;
9183
}
9284

@@ -112,14 +104,6 @@ namespace wiocellular
112104
if (dataSize < 1)
113105
return -1;
114106

115-
// // for debug
116-
// Serial.printf("W [%04X] ", address);
117-
// for (size_t i = 0; i < dataSize; ++i)
118-
// {
119-
// Serial.printf("%02X ", reinterpret_cast<const uint8_t *>(data)[i]);
120-
// }
121-
// Serial.println();
122-
123107
if (nrf_dma_accessible_check(nullptr, data))
124108
{
125109
if (Flash_.writeBuffer(address, reinterpret_cast<const uint8_t *>(data), dataSize) != dataSize)

src/internal/WioLog.hpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* WioLog.hpp
3+
* Copyright (C) Seeed K.K.
4+
* MIT License
5+
*/
6+
7+
#ifndef WIOLOG_HPP
8+
#define WIOLOG_HPP
9+
10+
#include <string>
11+
12+
namespace wiocellular::internal
13+
{
14+
15+
enum class WioLogType
16+
{
17+
INFO,
18+
WARNING,
19+
AT_CMD,
20+
AT_ECO,
21+
AT_INF,
22+
AT_FRC,
23+
AT_URC,
24+
AT_UNK,
25+
};
26+
27+
#if CFG_LOGGER != 3
28+
29+
#if 0 // 0: For plain text output, 1: For colored output
30+
31+
template <class... Args>
32+
static void WioLog(WioLogType type, const char *format, Args... args)
33+
{
34+
const char *buf;
35+
std::string str;
36+
if constexpr (sizeof...(Args) == 0)
37+
{
38+
buf = format;
39+
}
40+
else
41+
{
42+
const size_t len = snprintf(nullptr, 0, format, args...);
43+
str.resize(len + 1);
44+
snprintf(str.data(), len + 1, format, args...);
45+
str.resize(len);
46+
buf = str.c_str();
47+
}
48+
49+
switch (type)
50+
{
51+
case WioLogType::INFO:
52+
printf("\033[33mINFO: %s\033[0m\n", buf);
53+
break;
54+
case WioLogType::WARNING:
55+
printf("\033[35mWARNING: %s\033[0m\n", buf);
56+
break;
57+
case WioLogType::AT_CMD:
58+
printf("\033[32;1mCMD> %s\033[0m\n", buf);
59+
break;
60+
case WioLogType::AT_ECO:
61+
printf("\033[32mECO> %s\033[0m\n", buf);
62+
break;
63+
case WioLogType::AT_INF:
64+
printf("\033[32mINF> %s\033[0m\n", buf);
65+
break;
66+
case WioLogType::AT_FRC:
67+
printf("\033[32mFRC> %s\033[0m\n", buf);
68+
break;
69+
case WioLogType::AT_URC:
70+
printf("\033[32;1;7mURC> %s\033[0m\n", buf);
71+
break;
72+
case WioLogType::AT_UNK:
73+
printf("\033[31;1munk> %s\033[0m\n", buf);
74+
break;
75+
default:
76+
printf("%s\n", buf);
77+
break;
78+
}
79+
}
80+
81+
#else
82+
83+
template <class... Args>
84+
static void WioLog(WioLogType type, const char *format, Args... args)
85+
{
86+
const char *buf;
87+
std::string str;
88+
if constexpr (sizeof...(Args) == 0)
89+
{
90+
buf = format;
91+
}
92+
else
93+
{
94+
const size_t len = snprintf(nullptr, 0, format, args...);
95+
str.resize(len + 1);
96+
snprintf(str.data(), len + 1, format, args...);
97+
str.resize(len);
98+
buf = str.c_str();
99+
}
100+
101+
switch (type)
102+
{
103+
case WioLogType::INFO:
104+
printf("INFO: %s\n", buf);
105+
break;
106+
case WioLogType::WARNING:
107+
printf("WARNING: %s\n", buf);
108+
break;
109+
case WioLogType::AT_CMD:
110+
printf("CMD> %s\n", buf);
111+
break;
112+
case WioLogType::AT_ECO:
113+
printf("ECO> %s\n", buf);
114+
break;
115+
case WioLogType::AT_INF:
116+
printf("INF> %s\n", buf);
117+
break;
118+
case WioLogType::AT_FRC:
119+
printf("FRC> %s\n", buf);
120+
break;
121+
case WioLogType::AT_URC:
122+
printf("URC> %s\n", buf);
123+
break;
124+
case WioLogType::AT_UNK:
125+
printf("unk> %s\n", buf);
126+
break;
127+
default:
128+
printf("%s\n", buf);
129+
break;
130+
}
131+
}
132+
133+
#endif
134+
135+
#else
136+
137+
template <class... Args>
138+
static void WioLog(WioLogType type, const char *format, Args... args)
139+
{
140+
}
141+
142+
#endif
143+
144+
}
145+
146+
#endif // WIOLOG_HPP

0 commit comments

Comments
 (0)