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