C++ TargetRTS
Loading...
Searching...
No Matches
RTTracer.h
1/*******************************************************************************
2 * Licensed Materials - Property of HCL
3 * (c) Copyright HCL Technologies Ltd. 2025. All Rights Reserved.
4 *******************************************************************************/
5#ifndef __RTTracer_h__
6#define __RTTracer_h__ included
7
8#include <fstream>
9#include <string>
10#include <RTJsonResult.h>
11#include <RTStreamBuffer.h>
12#if RTUseCPP11
13#include <chrono>
14#endif
15
16class RTActor;
17class RTMessage;
18class RTProtocol;
19
20class RTTracer: public RTStreamBuffer {
21public:
25 static RTS_INLINE void flushTrace(void);
26
30 static RTS_INLINE bool isEnabled(void);
31
39 static void setEnabled(bool);
40
45 static bool configure(const char* configFile = nullptr);
46
50 static RTS_INLINE bool isConfigured(void);
51
55 static RTS_INLINE void note(const std::string& text);
56
61 Initialize, // Synchronous initialization of parts
62 Send, // Asynchronous send of a message
63 Invoke, // Synchronous invoke of a message (explicit reply)
64 InvokeWithoutReply, // Synchronous invoke of a message (implicit reply)
65 Reply, // Synchronous reply of a message
66 Pause, // Pause tracing
67 Flush // Flush the trace file
68 };
69
70#if RTUseCPP11
74 typedef std::chrono::system_clock::time_point TraceTime;
75#endif
76
80 struct TraceInfo {
81 const RTActor* sender;
82 const RTProtocol* fromPort;
83 int fromIndex;
84#if RTUseCPP11
85 TraceTime t2;
86#endif
87
88 TraceInfo();
89 };
90
94 static RTTracer& instance();
95
96protected:
97 enum {
98 TIME1 = 0x10, // 0001 0000
99 TIME2 = 0x20, // 0010 0000
100 TIME3 = 0x40, // 0100 0000
101 TIME_ABSOLUTE = 0x80, // 1000 0000
102 PRECISION_MASK = 0x0F, // 0000 1111
103 PRECISION_NANO = 0x09, // 0000 1001
104 PRECISION_MICRO = 0x06, // 0000 0110
105 PRECISION_MILLI = 0x03 // 0000 0011
106 };
107
108 static RTJsonResult jsonConfig;
109 static std::string _applicationName; // Name of traced application
110 static std::string _traceFileName; // Name of generated trace file
111#if RTTRACER_FLUSH_COUNT
112 static int _messagesSinceFlush;
113#endif
114 static bool _enabled;
115 static bool _configured;
116 static bool _overwriteFile;
117
118#if RTUseCPP11
119 static TraceTime _start;
120 static int _timestamps;
121 void printTimestamp(const TraceTime& timestamp);
122#endif
123
124 std::string _traceFile;
125 std::ofstream _file;
126
127 RTTracer(void);
128 ~RTTracer(void);
129
130 static std::string initTraceFile(void);
131
132 static void getSenderInfo(const RTMessage*, TraceInfo*);
133
134 static RTS_INLINE void traceMS(const RTMessage*, TraceEventKind traceEventKind, const TraceInfo* = nullptr);
135
136 void traceMessage(const RTMessage*, TraceEventKind traceEventKind, const TraceInfo*);
137 void traceNote(const std::string& text);
138 void traceNote_nolock(const std::string& text);
139 int flush(void) override;
140
141 friend class RTActor;
142 friend class RTMessage;
143 friend class RTProtocol;
144};
145
146#define TRACE_TIME1 "time1_send"
147#define TRACE_TIME2 "time2_receive"
148#define TRACE_TIME3 "time3_handle"
149
150#if RTS_INLINES
151#include <RTTracer.inl>
152#endif
153
154#endif
An instance of this class represents a capsule instance.
Definition: RTActor.h:44
Represents the result of parsing a JSON string.
Definition: RTJsonResult.h:30
Represents a message used for communication between capsule instances.
Definition: RTMessage.h:33
Represents a general capsule port typed by a protocol which determines the set of events that can be ...
Definition: RTProtocol.h:50
Definition: RTStreamBuffer.h:22
Definition: RTTracer.h:20
std::chrono::system_clock::time_point TraceTime
Type for storing trace timestamps.
Definition: RTTracer.h:74
static RTTracer & instance()
Get the singleton instance of RTTracer.
static bool isConfigured(void)
Check if tracing has been configured.
Definition: RTTracer.inl:20
static bool configure(const char *configFile=nullptr)
Configure RTTracer and tracing preferences.
static void flushTrace(void)
Flush buffered trace messages to file.
Definition: RTTracer.inl:12
static bool isEnabled(void)
Check if tracing is enabled.
Definition: RTTracer.inl:16
static void note(const std::string &text)
Write a note to the trace.
Definition: RTTracer.inl:31
TraceEventKind
Types of trace events supported.
Definition: RTTracer.h:60
static void setEnabled(bool)
Enable or disable tracing to file.
int flush(void) override
Flushes the data written to the buffer.
Structure with additional info passed to traceMessage call.
Definition: RTTracer.h:80