Advertisement
yoos

Untitled

Nov 3rd, 2011
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. //! @brief Get logfile name published by GUI and open file to write.
  2. bool serviceCallback(atrias_controllers::data_subscriber_srv::Request& req, atrias_controllers::data_subscriber_srv::Response& res) {
  3.     if (req.isLogging) {
  4.         ROS_INFO("logging");
  5.         char buffer[256];   // Create buffer to store filename.
  6.  
  7.         if (req.logfilename == "") {   // If filename is unspecified, set one based on date and time.
  8.             time_t curSeconds;
  9.             curSeconds = time(NULL);
  10.             struct tm *tInfo;
  11.             tInfo = localtime(&curSeconds);
  12.             sprintf(buffer, "%s/atrias_%0.2d%0.2d%0.2d_%0.2d%0.2d%0.2d.log", "/home/drl/atrias/drl-sim/atrias/log_files", tInfo->tm_year%100, tInfo->tm_mon+1, tInfo->tm_mday, tInfo->tm_hour, tInfo->tm_min, tInfo->tm_sec);
  13.         }  
  14.         else {   // If filename is specified, use that as logfilename.
  15.             sprintf(buffer, "%s", req.logfilename.c_str());
  16.         }  
  17.  
  18.         log_file_fp = fopen(buffer, "w");   // Open logfile.
  19.         fprintf(log_file_fp, "-");   // Make sure the file exists.
  20.         ROS_INFO("data_subscriber opened logfile at %s", buffer);
  21.         res.logfilename = buffer;   // Respond with new logfilename.
  22.         isLogging = true;   // data_subscriber should start logging.
  23.     }  
  24.     else if (!req.isLogging) {
  25.         ROS_INFO("not logging");
  26.         if (log_file_fp != NULL) {   // Check if logfile is open because serviceCallback could be run if logfilename is set but isLogging is off.
  27.             fclose(log_file_fp);
  28.             ROS_INFO("data_subscriber closed logfile.");
  29.         }  
  30.         res.logfilename = "";   // Respond with blank logfilename.
  31.     }  
  32.     return true;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement