PhoenixPresentation  2.0.0
Set of cmake function to automate presentation generation
phoenix_system.h File Reference
#include <time.h>
#include <chrono>
#include <ctime>
#include "PPath.h"
+ Include dependency graph for phoenix_system.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef std::chrono::steady_clock PhoenixClock
 
typedef std::chrono::nanoseconds PhoenixNs
 
typedef std::chrono::time_point< PhoenixClockPhoenixTime
 

Functions

time_t phoenix_getClock ()
 Get current time. More...
 
time_t phoenix_getClockNs ()
 Get current time in nanosecond since the starting of the CPU (steady_clock) More...
 
double phoenix_getClockSec ()
 Get current time. More...
 
PString phoenix_getDate ()
 Get the current date. More...
 
PString phoenix_getDateCompact ()
 Get the current date. More...
 
short unsigned int phoenix_getNbColTerminal ()
 Get the number of columns of the terminal. More...
 
short unsigned int phoenix_getNbRowTerminal ()
 Get the number of rows of the terminal. More...
 
time_t phoenix_getTime ()
 Get the current time of the program. More...
 
bool phoenix_popen (const PPath &executionLogFile, const PString &command, bool onlyLogOnFail)
 Execute the given command and returns the output of this command. More...
 
PString phoenix_popen (const PString &command)
 Execute the given command and returns the output of this command. More...
 
int phoenix_popen (PString &executionLog, const PString &command)
 Execute the given command and returns the output of this command. More...
 
PString phoenix_whoami ()
 Get the name of the current user. More...
 

Typedef Documentation

◆ PhoenixClock

typedef std::chrono::steady_clock PhoenixClock

Definition at line 15 of file phoenix_system.h.

◆ PhoenixNs

typedef std::chrono::nanoseconds PhoenixNs

Definition at line 16 of file phoenix_system.h.

◆ PhoenixTime

typedef std::chrono::time_point<PhoenixClock> PhoenixTime

Definition at line 17 of file phoenix_system.h.

Function Documentation

◆ phoenix_getClock()

time_t phoenix_getClock ( )

Get current time.

Returns
current time

Definition at line 61 of file phoenix_system.cpp.

61  {
62  return clock();
63 }

Referenced by phoenix_getClockSec(), and testPhoenixSystemTime().

+ Here is the caller graph for this function:

◆ phoenix_getClockNs()

time_t phoenix_getClockNs ( )

Get current time in nanosecond since the starting of the CPU (steady_clock)

Returns
current time in nanosecond

Definition at line 68 of file phoenix_system.cpp.

68  {
69  std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now();
70  auto duration = now.time_since_epoch();
71  auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
72  return nanoseconds.count();
73 
74 
75 // return std::chrono::steady_clock::to_time_t(PhoenixClock::now());
76 
77 // return std::chrono::system_clock::to_time_t((std::chrono::time_point<std::chrono::system_clock>)PhoenixClock::now());
78 
79  //This compiles, but it does not have the precision I want
80 // return std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
81 }

Referenced by testPhoenixSystemTime().

+ Here is the caller graph for this function:

◆ phoenix_getClockSec()

double phoenix_getClockSec ( )

Get current time.

Returns
current time

Definition at line 86 of file phoenix_system.cpp.

86  {
87  return ((double)phoenix_getClock())/((double)CLOCKS_PER_SEC);
88 }
time_t phoenix_getClock()
Get current time.

References phoenix_getClock().

Referenced by testPhoenixSystemTime().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_getDate()

PString phoenix_getDate ( )

Get the current date.

Returns
current date

Definition at line 100 of file phoenix_system.cpp.

100  {
101  std::time_t currentTime = phoenix_getTime();
102  std::tm* now_tm = std::gmtime(&currentTime);
103  char buf[42];
104  std::strftime(buf, 42, "%Y/%m/%d : %X", now_tm);
105  return buf;
106 }
time_t phoenix_getTime()
Get the current time of the program.

References phoenix_getTime().

Referenced by PLog::close(), PLog::open(), and testPhoenixSystemTime().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_getDateCompact()

PString phoenix_getDateCompact ( )

Get the current date.

Returns
current date

Definition at line 111 of file phoenix_system.cpp.

111  {
112  std::time_t currentTime = phoenix_getTime();
113  std::tm* now_tm = std::gmtime(&currentTime);
114  char buf[42];
115  std::strftime(buf, 42, "%Y/%m/%d-%X", now_tm);
116  return buf;
117 }

References phoenix_getTime().

Referenced by PLog::getLog(), and testPhoenixSystemTime().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_getNbColTerminal()

short unsigned int phoenix_getNbColTerminal ( )

Get the number of columns of the terminal.

Returns
number of columns of the terminal

Definition at line 133 of file phoenix_system.cpp.

133  {
134  struct winsize w;
135  ioctl(0, TIOCGWINSZ, &w);
136  return w.ws_col;
137 }

Referenced by testPhoenixSystemTime().

+ Here is the caller graph for this function:

◆ phoenix_getNbRowTerminal()

short unsigned int phoenix_getNbRowTerminal ( )

Get the number of rows of the terminal.

Returns
number of rows of the terminal

Definition at line 142 of file phoenix_system.cpp.

142  {
143  struct winsize w;
144  ioctl(0, TIOCGWINSZ, &w);
145  return w.ws_row;
146 }

Referenced by testPhoenixSystemTime().

+ Here is the caller graph for this function:

◆ phoenix_getTime()

time_t phoenix_getTime ( )

Get the current time of the program.

Returns
current time of the program

Definition at line 93 of file phoenix_system.cpp.

93  {
94  return std::time(0);
95 }

Referenced by PLog::getLog(), phoenix_getDate(), phoenix_getDateCompact(), and testPhoenixSystemTime().

+ Here is the caller graph for this function:

◆ phoenix_popen() [1/3]

bool phoenix_popen ( const PPath executionLogFile,
const PString command,
bool  onlyLogOnFail 
)

Execute the given command and returns the output of this command.

Parameters
[out]executionLogFile: file which will get output of the given command, empty string if the command is empty or full log on fail
command: command to be executed
onlyLogOnFail: true to log only if the command fails
Returns
true if the command was successful, false otherwise (in this case, log file will be created)

Definition at line 49 of file phoenix_system.cpp.

49  {
50  PString executionLog("");
51  bool b(phoenix_popen(executionLog, command) == 0);
52  if((onlyLogOnFail && !b) || !onlyLogOnFail){
53  executionLogFile.saveFileContent(executionLog);
54  }
55  return b;
56 }
bool saveFileContent(const PString &content) const
Save a PString in a file.
Definition: PPath.cpp:395
Extends the std::string.
Definition: PString.h:16
PString phoenix_popen(const PString &command)
Execute the given command and returns the output of this command.

References phoenix_popen(), and PPath::saveFileContent().

+ Here is the call graph for this function:

◆ phoenix_popen() [2/3]

PString phoenix_popen ( const PString command)

Execute the given command and returns the output of this command.

Parameters
command: command to be executed
Returns
output of the given command, empty string if the command is empty or null character on fail

Definition at line 19 of file phoenix_system.cpp.

19  {
20  if(command == ""){return "";}
21  FILE * fp = popen(command.c_str(), "r");
22  if(fp == NULL){return "";}
23  PString resultCommand(phoenix_getFileContent(fp));
24  pclose(fp);
25  return resultCommand;
26 }
PString phoenix_getFileContent(FILE *fp)
Get the content of a file.
Definition: PPath.cpp:28

References phoenix_getFileContent().

Referenced by checkOptionCompletion(), PPath::getCurrentNodeName(), phoenix_popen(), and testPhoenixPOpen().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_popen() [3/3]

int phoenix_popen ( PString executionLog,
const PString command 
)

Execute the given command and returns the output of this command.

Parameters
[out]executionLog: output of the given command, empty string if the command is empty or null character on fail
command: command to be executed
Returns
exit status of the command

Definition at line 33 of file phoenix_system.cpp.

33  {
34  executionLog = "";
35  if(command == ""){return -1;}
36  FILE * fp = popen(command.c_str(), "r");
37  if(fp == NULL){return -1;}
38  executionLog = phoenix_getFileContent(fp);
39  return pclose(fp);
40 }

References phoenix_getFileContent().

+ Here is the call graph for this function:

◆ phoenix_whoami()

PString phoenix_whoami ( )

Get the name of the current user.

Returns
name of the current user

Definition at line 122 of file phoenix_system.cpp.

122  {
123  std::string str("");
124  str.resize(1024lu);
125  getlogin_r((char*)str.data(), str.size() - 1lu);
126  return str;
127 }

Referenced by testPhoenixWhoAmI().

+ Here is the caller graph for this function: