YARP
Yet Another Robot Platform
RunCheckpoints.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
10 
11 #include <ctime>
12 #include <cstdio>
13 
14 #if defined(_WIN32)
15 #include <windows.h>
16 #endif
17 
18 
19 YarprunCheckpoints::YarprunCheckpoints()
20 {
21  char path[256];
22 
23 #if defined(_WIN32)
24  time_t now=time(nullptr);
25  srand((unsigned)now);
26  sprintf(path, "C:/Users/user/Documents/yarprun_log/yarprun_log_%d_%s_%u.txt", GetCurrentProcessId(), ctime(&now), (unsigned)rand());
27 #else
28  timeval now;
29  gettimeofday(&now, nullptr);
30  sprintf(path, "/tmp/yarprun_log_%d_%s_%06d.txt", getpid(), ctime(&(now.tv_sec)), (int)now.tv_usec);
31 #endif
32 
33  for (int t=10; t<256 && path[t]; ++t)
34  {
35  if (path[t] == '\n' || path[t] == '\r' || path[t] == ' ' || path[t] == ':' || path[t] == '?') {
36  path[t] = '_';
37  }
38  }
39 
40  mLogFile=fopen(path, "w");
41 
42  if (!mLogFile) {
43  perror(path);
44  }
45 }
46 
48 {
49  if (mLogFile) {
50  fclose(mLogFile);
51  }
52 }
53 
55 {
56  static YarprunCheckpoints singleton;
57 
58  return singleton;
59 }
60 
61 void YarprunCheckpoints::checkpoint(const char *prefix, const char* sFile, const char* sFunction, int line)
62 {
63  if (!mLogFile) {
64  return;
65  }
66 
67  fprintf(mLogFile, "%s: file %s function %s line %d\n", prefix, sFile, sFunction, line);
68  fflush(mLogFile);
69 }
float t
static YarprunCheckpoints & instance()
void checkpoint(const char *prefix, const char *sFile, const char *sFunction, int line)
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121
int getpid()
Portable wrapper for the getppid() function.
Definition: Os.cpp:91