44#include < netdb.h>
55#include < netinet/in.h>
66#include < sys/socket.h>
7+ #include < sys/stat.h>
8+ #include < sys/types.h>
79
810#include < chrono>
911#include < csignal>
@@ -30,17 +32,20 @@ using std::vector;
3032
3133atomic<bool > running_program (true );
3234void InterruptSignalHandler (int signal);
35+ bool checkDirectory (const std::string& path);
36+ bool createDirectory (const std::string& path);
3337
3438int main (int argc, char * argv[]) {
3539 const char * server_address = " 0.0.0.0" ;
3640 const int kDefaultPortNum = 5500 ;
3741 const int kBase = 10 ; // Named constant for base 10
3842 int server_portnum = kDefaultPortNum ;
3943 int log_level = 1 ;
44+ std::string logging_directory = " ./log" ;
4045
4146 // Command-line options
4247 int opt = 0 ;
43- while ((opt = getopt (argc, argv, " a:p:l:" )) != -1 ) {
48+ while ((opt = getopt (argc, argv, " a:p:l:o: " )) != -1 ) {
4449 switch (opt) {
4550 case ' a' :
4651 server_address = optarg;
@@ -51,16 +56,21 @@ int main(int argc, char* argv[]) {
5156 case ' l' :
5257 log_level = strtol (optarg, nullptr , kBase );
5358 break ;
59+ case ' o' :
60+ logging_directory = optarg;
61+ break ;
5462 default :
5563 std::cout << " Usage: " << argv[0 ];
5664 std::cout << " [-a server_address] " ;
5765 std::cout << " [-p server_portnum]" ;
5866 std::cout << " [-l log level]" ;
67+ std::cout << " [-o output direcotry. Example \" ./log\" ]" ;
5968 return EXIT_FAILURE;
6069 }
6170 }
6271 // sem_init(&stopProgramSem, 0, 0);
6372 /* Glog initializing */
73+
6474 google::InitGoogleLogging (argv[0 ]);
6575 if (log_level == 0 ) {
6676 FLAGS_stderrthreshold = 0 ;
@@ -70,7 +80,15 @@ int main(int argc, char* argv[]) {
7080 } else {
7181 FLAGS_stderrthreshold = 0 ;
7282 }
73- FLAGS_log_dir = " ./log" ;
83+
84+ if (!checkDirectory (logging_directory)) {
85+ if (!createDirectory (logging_directory)) {
86+ std::cout << " Failed to create directory: " << logging_directory << ' \n ' ;
87+ return EXIT_FAILURE;
88+ }
89+ }
90+
91+ FLAGS_log_dir = logging_directory;
7492 LOG_INFO << " Logger was set up." << " Directory to log: " << FLAGS_log_dir;
7593
7694 MotorController motors_processor;
@@ -132,4 +150,20 @@ int main(int argc, char* argv[]) {
132150 return 0 ;
133151}
134152
153+ bool checkDirectory (const std::string& path) {
154+ struct stat info;
155+ return stat (path.c_str (), &info) == 0 && (info.st_mode & S_IFDIR);
156+ }
157+
158+ bool createDirectory (const std::string& path) {
159+ mode_t mode = 0755 ;
160+ int ret = mkdir (path.c_str (), mode);
161+ if (ret == 0 || errno == EEXIST) {
162+ return true ;
163+ } else {
164+ std::cerr << " mkdir error: " << std::strerror (errno) << ' \n ' ;
165+ return false ;
166+ }
167+ }
168+
135169void InterruptSignalHandler (int signal) { running_program = false ; }
0 commit comments