AJA NTV2 SDK  17.0.1.1246
NTV2 SDK 17.0.1.1246
log.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
8 //#include "ajabase/system/systemtime.h"
9 #include "ajabase/system/log.h"
11 
12 bool AJALog::bInitialized = false;
13 
14 #if defined(AJA_WINDOWS)
15 #include <Windows.h>
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <ctype.h>
19 
20 void __cdecl log_odprintf(const char *format, ...)
21 {
22  char buf[4096], *p = buf;
23  va_list args;
24 
25  va_start(args, format);
26  p += _vsnprintf_s(buf, sizeof buf - 1, format, args);
27  va_end(args);
28 
29  while ( p > buf && isspace(p[-1]) )
30  *--p = '\0';
31 
32  *p++ = '\r';
33  *p++ = '\n';
34  *p = '\0';
35 
36  ::OutputDebugStringA(buf);
37 }
38 #endif
39 
40 //---------------------------------------------------------------------------------------------------------------------
41 // class AJALog
42 //---------------------------------------------------------------------------------------------------------------------
43 
44 // singleton initialization here
46 {
47  if (bInitialized == false)
48  {
49  bInitialized = true;
50 
51  #if defined(AJA_MAC)
52 
53  #if (AJA_LOGTYPE==2)
55 
56  #endif
57 
58  #elif defined(AJA_LINUX)
59 
60  // one-time initialization here as needed
61 
62  #elif defined(AJA_WINDOWS)
63 
64  #if (AJA_LOGTYPE==2)
66  #endif
67 
68  // one-time initialization here as needed
69 
70  #endif
71  }
72 }
73 
74 // perform singleton release here
76 {
77 }
78 
79 
80 //---------------------------------------------------------------------------------------------------------------------
81 // MARK: - AJARunAverage
82 //---------------------------------------------------------------------------------------------------------------------
83 
84 void AJARunAverage::Mark(int64_t val)
85 {
86  uint64_t index = _samplesTotal++ % _sampleSize;
87  _samples[index] = val;
88 }
89 
90 int64_t AJARunAverage::MarkAverage(int64_t val)
91 {
92  Mark(val);
93  return Average();
94 }
95 
97 {
98  if (_samplesTotal == 0)
99  return -1;
100  uint64_t lastIndex = ((_samplesTotal-1) % _sampleSize);
101  return _samples[lastIndex];
102 }
103 
105 {
106  uint64_t sampleSize = _samplesTotal < _sampleSize ? _samplesTotal : _sampleSize;
107  if (sampleSize == 0)
108  return 0;
109 
110  int64_t average = 0;
111  for (uint64_t i=0; i <sampleSize; i++)
112  average += _samples[i];
113 
114  average = average / sampleSize;
115  return average;
116 }
117 
119 {
120  _samplesTotal = 0;
121  std::fill(_samples.begin(), _samples.end(), 0);
122 }
123 
124 void AJARunAverage::Resize(uint64_t sampleSize)
125 {
126  _sampleSize = sampleSize;
127  _samples.resize(sampleSize);
128  Reset();
129 }
130 
131 
132 
133 //---------------------------------------------------------------------------------------------------------------------
134 // MARK: - AJARunTimeAverage
135 // calculates a running average of time deltas
136 //---------------------------------------------------------------------------------------------------------------------
137 
139 {
140  ResetTime();
141 }
142 
143 void AJARunTimeAverage::Resize(uint64_t sampleSize)
144 {
145  AJARunAverage::Resize(sampleSize);
146 }
147 
149 {
151  ResetTime();
152 }
153 
155 {
157 }
158 
159 // mark current delta-time
160 // return delta-time
162 {
163  int64_t currTime = (int64_t) AJATime::GetSystemMicroseconds();
164  int64_t deltaTime = currTime - _lastTime;
165  _lastTime = currTime;
166 
167  Mark(deltaTime);
168  return deltaTime;
169 }
170 
171 // mark current delta-time
172 // return running average delta-time
174 {
175  int64_t currTime = (int64_t) AJATime::GetSystemMicroseconds();
176  int64_t deltaTime = currTime - _lastTime;
177  _lastTime = currTime;
178 
179  Mark(deltaTime);
180  return Average();
181 }
182 
183 
184 //---------------------------------------------------------------------------------------------------------------------
185 // MARK: - class AJATimeLog
186 //---------------------------------------------------------------------------------------------------------------------
187 
189 {
190  _tag = "";
192  Reset();
193 }
194 
195 AJATimeLog::AJATimeLog(const char* tag, int unit)
196 {
197  _unit = unit;
198  _tag = tag;
199  Reset();
200 }
201 
202 AJATimeLog::AJATimeLog(const std::string& tag, int unit)
203 {
204  _unit = unit;
205  _tag = tag;
206  Reset();
207 }
208 
210 {
211 }
212 
213 // reset time
215 {
217  _lastDelta = 0;
218 }
219 
220 // reset time
222 {
223  Reset();
224  PrintValue(0, "(** Reset **)");
225 }
226 
227 // print dela time in micro seconds
228 void AJATimeLog::PrintDelta(bool bReset)
229 {
230  uint64_t delta = GetDelta(bReset);
231  PrintValue(delta);
232 }
233 
234 // print dela time in micro seconds
235 int32_t AJATimeLog::GetDelta(bool bReset)
236 {
237  uint64_t currTime = AJATime::GetSystemMicroseconds();
238  _lastDelta = currTime - _time;
239  if (bReset)
240  _time = currTime;
241  return (int32_t)_lastDelta;
242 }
243 
244 // print delta time in micro seconds, use additional tag
245 void AJATimeLog::PrintDelta(const char* addedTag, bool bReset)
246 {
247  uint64_t delta = GetDelta(bReset);
248  PrintValue(delta, addedTag);
249 }
250 
251 void AJATimeLog::PrintDelta(uint64_t threashold, const char* addedTag, bool bReset)
252 {
253  uint64_t delta = GetDelta(bReset);
254  if (delta > threashold)
255  PrintValue(delta, addedTag);
256 }
257 
258 void AJATimeLog::PrintValue(int64_t val)
259 {
260  #if defined(AJA_DEBUG) && (AJA_LOGTYPE!=2)
262  AJA_LOG("%s = %lld\n", _tag.c_str(), val);
263  #else
265  AJADebug::Report(_unit, AJA_DebugSeverity_Debug, __FILE__, __LINE__, "%s = %lld", _tag.c_str(), val);
266  #endif
267 }
268 
269 
270 void AJATimeLog::PrintValue(int64_t val, const char* addedTag)
271 {
272  #if defined(AJA_DEBUG) && (AJA_LOGTYPE!=2)
274  AJA_LOG("%s-%s = %lld\n", _tag.c_str(), addedTag, val);
275  #else
277  AJADebug::Report(_unit, AJA_DebugSeverity_Debug, __FILE__, __LINE__, "%s-%s = %lld", _tag.c_str(), addedTag, val);
278  #endif
279 }
280 
281 void AJATimeLog::Print(const char* str)
282 {
283  #if defined(AJA_DEBUG) && (AJA_LOGTYPE!=2)
285  AJA_LOG("%s-%s\n", _tag.c_str(), str);
286  #else
288  AJADebug::Report(_unit, AJA_DebugSeverity_Debug, __FILE__, __LINE__, "%s-%s", _tag.c_str(), str);
289  #endif
290 }
AJARunAverage::Mark
void Mark(int64_t val)
Definition: log.cpp:84
AJALog::AJALog
AJALog()
Definition: log.cpp:45
AJARunAverage::LastValue
int64_t LastValue()
Definition: log.cpp:96
AJATimeLog::Print
void Print(const char *str)
Definition: log.cpp:281
AJATimeLog::PrintReset
void PrintReset()
Definition: log.cpp:221
AJARunTimeAverage::Reset
virtual void Reset()
Definition: log.cpp:148
AJARunTimeAverage::MarkDeltaAverage
int64_t MarkDeltaAverage()
Definition: log.cpp:173
systemtime.h
Declares the AJATime class.
AJARunAverage::Reset
virtual void Reset()
Definition: log.cpp:118
AJADebug::Report
static void Report(int32_t index, int32_t severity, const char *pFileName, int32_t lineNumber,...)
Definition: debug.cpp:388
AJATime::GetSystemMicroseconds
static uint64_t GetSystemMicroseconds(void)
Returns the current value of the host's high-resolution clock, in microseconds.
Definition: systemtime.cpp:221
AJARunAverage
Definition: log.h:136
AJARunTimeAverage::MarkDeltaTime
int64_t MarkDeltaTime()
Definition: log.cpp:161
AJARunAverage::Average
int64_t Average()
Definition: log.cpp:104
AJATimeLog::_lastDelta
uint64_t _lastDelta
Definition: log.h:270
AJADebug::IsActive
static bool IsActive(int32_t index)
Definition: debug.cpp:278
AJATimeLog::PrintValue
void PrintValue(int64_t val)
Definition: log.cpp:258
AJARunAverage::_sampleSize
uint64_t _sampleSize
Definition: log.h:141
AJATimeLog::_tag
std::string _tag
Definition: log.h:267
AJA_DebugSeverity_Debug
@ AJA_DebugSeverity_Debug
Definition: debugshare.h:32
AJATimeLog::_unit
int _unit
Definition: log.h:268
AJARunTimeAverage::Resize
virtual void Resize(uint64_t sampleSize)
Definition: log.cpp:143
AJATimeLog::PrintDelta
void PrintDelta(bool bReset=true)
Definition: log.cpp:228
AJARunTimeAverage::_lastTime
int64_t _lastTime
Definition: log.h:170
AJATimeLog::GetDelta
int32_t GetDelta(bool bReset=true)
Definition: log.cpp:235
AJARunAverage::Resize
virtual void Resize(uint64_t sampleSize)
Definition: log.cpp:124
AJADebug::Open
static AJAStatus Open(bool incrementRefCount=false)
Definition: debug.cpp:44
AJARunTimeAverage::AJARunTimeAverage
AJARunTimeAverage()
Definition: log.h:169
AJA_DebugUnit_Critical
@ AJA_DebugUnit_Critical
Definition: debugshare.h:47
log.h
Declares the AJATimeLog class.
AJATimeLog::AJATimeLog
AJATimeLog()
Definition: log.cpp:188
AJATimeLog::_time
uint64_t _time
Definition: log.h:269
AJALog::~AJALog
virtual ~AJALog()
Definition: log.cpp:75
AJATimeLog::~AJATimeLog
virtual ~AJATimeLog()
Definition: log.cpp:209
AJARunAverage::_samplesTotal
uint64_t _samplesTotal
Definition: log.h:140
AJARunAverage::_samples
std::vector< int64_t > _samples
Definition: log.h:142
AJARunAverage::MarkAverage
int64_t MarkAverage(int64_t val)
Definition: log.cpp:90
AJARunTimeAverage::ResetTime
virtual void ResetTime()
Definition: log.cpp:154
AJATimeLog::Reset
void Reset()
Definition: log.cpp:214