Skip to content

Commit

Permalink
DEVLOG: Option for human readable time
Browse files Browse the repository at this point in the history
  • Loading branch information
kumajaya authored and azoitl committed Apr 29, 2024
1 parent c8c9d35 commit 3d80e37
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/arch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ SET(FORTE_LOGGER_BUFFER_SIZE "300" CACHE STRING "Buffer's length of the logger")
mark_as_advanced(FORTE_LOGGER_BUFFER_SIZE)
forte_add_custom_configuration("#define FORTE_LOGGER_BUFFER_SIZE ${FORTE_LOGGER_BUFFER_SIZE}")

SET(FORTE_LOGGER_READABLE_TIME OFF CACHE BOOL "Logger time in IEC 61131-3 date time literal")
if(FORTE_LOGGER_READABLE_TIME)
forte_add_sourcefile_cpp(readableTimeString.cpp)
else()
forte_add_sourcefile_cpp(plainTimeString.cpp)
endif()

if (FORTE_STACKTRACE)
if (FORTE_STACKTRACE_IMPL MATCHES "boost")
find_package(Boost REQUIRED)
Expand Down
4 changes: 3 additions & 1 deletion src/arch/devlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#endif // FORTE_STACKTRACE_CXX23
#endif // FORTE_STACKTRACE

std::string getRealtimeString();

static const char* scLogLevel[] = { "INFO", "WARNING", "ERROR", "DEBUG", "TRACE" };

//this define allows to provide an own log handler (see LMS for an example of this)
Expand Down Expand Up @@ -98,7 +100,7 @@ void logMessage(E_MsgLevel paLevel, const char *paMessage, ...) {

void printLogMessage(E_MsgLevel paLevel, const char *paMessage) {
#ifndef __ZEPHYR__
(paLevel == E_MsgLevel::Error ? std::cerr : std::cout) << scLogLevel[static_cast<int>(paLevel)] << ": " << getNanoSecondsMonotonic() << ": " << paMessage;
(paLevel == E_MsgLevel::Error ? std::cerr : std::cout) << scLogLevel[static_cast<int>(paLevel)] << ": " << getRealtimeString() << ": " << paMessage;
#ifdef WIN32
(paLevel == E_MsgLevel::Error ? std::cerr : std::cout) << std::flush;
#endif
Expand Down
20 changes: 20 additions & 0 deletions src/arch/plainTimeString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2024 Samator Indo Gas
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Ketut Kumajaya - initial API and implementation and/or initial documentation
*******************************************************************************/

#include "forte_architecture_time.h"
#include <sstream>

std::string getRealtimeString() {
std::ostringstream stream;
stream << "T#" << getNanoSecondsRealtime();
return stream.str();
}
20 changes: 20 additions & 0 deletions src/arch/readableTimeString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2024 Samator Indo Gas
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Ketut Kumajaya - initial API and implementation and/or initial documentation
*******************************************************************************/

#include "../core/iec61131_functions.h"

std::string getRealtimeString() {
CIEC_DATE_AND_TIME dt = func_NOW();
std::string str(dt.getToStringBufferSize(), '\0');
dt.toString(str.data(), str.size());
return str;
}

0 comments on commit 3d80e37

Please sign in to comment.