Directory: | ./ |
---|---|
File: | tmp_project/PhoenixCore/src/phoenix_system.cpp |
Date: | 2025-03-14 11:56:07 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 55 | 55 | 100.0% |
Branches: | 30 | 34 | 88.2% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <stdio.h> | ||
8 | #include <ctime> | ||
9 | #include "unistd.h" | ||
10 | #include <sys/ioctl.h> | ||
11 | |||
12 | #include "PPath.h" | ||
13 | #include "phoenix_system.h" | ||
14 | |||
15 | ///Execute the given command and returns the output of this command | ||
16 | /** @param command : command to be executed | ||
17 | * @return output of the given command, empty string if the command is empty or null character on fail | ||
18 | */ | ||
19 | 32 | PString phoenix_popen(const PString & command){ | |
20 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 2 taken 31 times.
✓ Branch 4 taken 1 times.
|
32 | if(command == ""){return "";} |
21 |
1/1✓ Branch 2 taken 31 times.
|
31 | FILE * fp = popen(command.c_str(), "r"); |
22 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
31 | if(fp == NULL){return "";} |
23 |
1/1✓ Branch 1 taken 31 times.
|
31 | PString resultCommand(phoenix_getFileContent(fp)); |
24 |
1/1✓ Branch 1 taken 31 times.
|
31 | pclose(fp); |
25 |
1/1✓ Branch 1 taken 31 times.
|
31 | return resultCommand; |
26 | 31 | } | |
27 | |||
28 | ///Execute the given command and returns the output of this command | ||
29 | /** @param[out] executionLog : output of the given command, empty string if the command is empty or null character on fail | ||
30 | * @param command : command to be executed | ||
31 | * @return exit status of the command | ||
32 | */ | ||
33 | 7 | int phoenix_popen(PString & executionLog, const PString & command){ | |
34 | 7 | executionLog = ""; | |
35 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
|
7 | if(command == ""){return -1;} |
36 | 6 | FILE * fp = popen(command.c_str(), "r"); | |
37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if(fp == NULL){return -1;} |
38 |
1/1✓ Branch 2 taken 6 times.
|
6 | executionLog = phoenix_getFileContent(fp); |
39 | 6 | return pclose(fp); | |
40 | } | ||
41 | |||
42 | |||
43 | ///Execute the given command and returns the output of this command | ||
44 | /** @param[out] executionLogFile : file which will get output of the given command, empty string if the command is empty or full log on fail | ||
45 | * @param command : command to be executed | ||
46 | * @param onlyLogOnFail : true to log only if the command fails | ||
47 | * @return true if the command was successful, false otherwise (in this case, log file will be created) | ||
48 | */ | ||
49 | 4 | bool phoenix_popen(const PPath & executionLogFile, const PString & command, bool onlyLogOnFail){ | |
50 |
1/1✓ Branch 1 taken 4 times.
|
4 | PString executionLog(""); |
51 |
1/1✓ Branch 1 taken 4 times.
|
4 | bool b(phoenix_popen(executionLog, command) == 0); |
52 |
6/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
|
4 | if((onlyLogOnFail && !b) || !onlyLogOnFail){ |
53 |
1/1✓ Branch 1 taken 3 times.
|
3 | executionLogFile.saveFileContent(executionLog); |
54 | } | ||
55 | 4 | return b; | |
56 | 4 | } | |
57 | |||
58 | ///Get current time | ||
59 | /** @return current time | ||
60 | */ | ||
61 | 2 | time_t phoenix_getClock(){ | |
62 | 2 | return clock(); | |
63 | } | ||
64 | |||
65 | ///Get current time in nanosecond since the starting of the CPU (steady_clock) | ||
66 | /** @return current time in nanosecond | ||
67 | */ | ||
68 | 3 | time_t phoenix_getClockNs(){ | |
69 | 3 | std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now(); | |
70 | 3 | auto duration = now.time_since_epoch(); | |
71 |
1/1✓ Branch 1 taken 3 times.
|
3 | auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(duration); |
72 | 6 | 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 | } | ||
82 | |||
83 | ///Get current time | ||
84 | /** @return current time | ||
85 | */ | ||
86 | 1 | double phoenix_getClockSec(){ | |
87 | 1 | return ((double)phoenix_getClock())/((double)CLOCKS_PER_SEC); | |
88 | } | ||
89 | |||
90 | ///Get the current time of the program | ||
91 | /** @return current time of the program | ||
92 | */ | ||
93 | 207 | time_t phoenix_getTime(){ | |
94 | 207 | return std::time(0); | |
95 | } | ||
96 | |||
97 | ///Get the current date | ||
98 | /** @return current date | ||
99 | */ | ||
100 | 25 | PString phoenix_getDate(){ | |
101 |
1/1✓ Branch 1 taken 25 times.
|
25 | std::time_t currentTime = phoenix_getTime(); |
102 | 25 | std::tm* now_tm = std::gmtime(¤tTime); | |
103 | char buf[42]; | ||
104 | 25 | std::strftime(buf, 42, "%Y/%m/%d : %X", now_tm); | |
105 |
1/1✓ Branch 1 taken 25 times.
|
50 | return buf; |
106 | } | ||
107 | |||
108 | ///Get the current date | ||
109 | /** @return current date | ||
110 | */ | ||
111 | 91 | PString phoenix_getDateCompact(){ | |
112 |
1/1✓ Branch 1 taken 91 times.
|
91 | std::time_t currentTime = phoenix_getTime(); |
113 | 91 | std::tm* now_tm = std::gmtime(¤tTime); | |
114 | char buf[42]; | ||
115 | 91 | std::strftime(buf, 42, "%Y/%m/%d-%X", now_tm); | |
116 |
1/1✓ Branch 1 taken 91 times.
|
182 | return buf; |
117 | } | ||
118 | |||
119 | ///Get the name of the current user | ||
120 | /** @return name of the current user | ||
121 | */ | ||
122 | 1 | PString phoenix_whoami(){ | |
123 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string str(""); |
124 |
1/1✓ Branch 1 taken 1 times.
|
1 | str.resize(1024lu); |
125 |
1/1✓ Branch 3 taken 1 times.
|
1 | getlogin_r((char*)str.data(), str.size() - 1lu); |
126 |
1/1✓ Branch 1 taken 1 times.
|
2 | return str; |
127 | 1 | } | |
128 | |||
129 | |||
130 | ///Get the number of columns of the terminal | ||
131 | /** @return number of columns of the terminal | ||
132 | */ | ||
133 | 1 | short unsigned int phoenix_getNbColTerminal(){ | |
134 | struct winsize w; | ||
135 | 1 | ioctl(0, TIOCGWINSZ, &w); | |
136 | 1 | return w.ws_col; | |
137 | } | ||
138 | |||
139 | ///Get the number of rows of the terminal | ||
140 | /** @return number of rows of the terminal | ||
141 | */ | ||
142 | 1 | short unsigned int phoenix_getNbRowTerminal(){ | |
143 | struct winsize w; | ||
144 | 1 | ioctl(0, TIOCGWINSZ, &w); | |
145 | 1 | return w.ws_row; | |
146 | } | ||
147 | |||
148 | |||
149 |