Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/src/recognition/logger/file_logger.cppGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 // @author Brett Ponsler (bponsler (at) wpi (dot) edu) 00035 #include "file_logger.h" 00036 00037 #include <ros/ros.h> 00038 #include <stdarg.h> 00039 #include <stdio.h> 00040 #include "../lib/engagement_params.h" 00041 00042 // Create the instance of the FileLogger 00043 FileLogger *FileLogger::logger_ = NULL; 00044 00045 FileLogger::FileLogger() 00046 { 00047 // Set the directory for the logger 00048 this->directory_ = EngagementParams::getLogDirectory(); 00049 this->setLogger("[ FLOG]: "); 00050 00051 // Create and initialize the mutex 00052 memset(&(this->write_mutex_), 0, sizeof(pthread_mutex_t)); 00053 pthread_mutex_init(&(this->write_mutex_), NULL); 00054 } 00055 00056 FileLogger *FileLogger::getInstance() 00057 { 00058 // Determine if the instance already exists 00059 if (FileLogger::logger_ == NULL) 00060 { 00061 // Does not exists, create it 00062 FileLogger::logger_ = new FileLogger(); 00063 } 00064 00065 // Return the instance 00066 return FileLogger::logger_; 00067 } 00068 00069 void FileLogger::setDirectory(std::string directory) 00070 { 00071 // Grab the instance of the FileLogger class 00072 FileLogger *logger = FileLogger::getInstance(); 00073 00074 // Make sure the logger exists 00075 if (logger) 00076 { 00077 // Set the directory for this logger 00078 logger->directory_ = std::string(directory); 00079 } 00080 } 00081 00082 int FileLogger::openFile(std::string filename) 00083 { 00084 // Traverse through all the known filenames 00085 for (unsigned c = 0; c < this->filenames_.size(); c++) 00086 { 00087 // Check to see if this filename already exists 00088 if (filenames_.at(c) == filename) 00089 return c; 00090 } 00091 00092 // Create the new file, add the filename, and the file descriptor 00093 filenames_.push_back(filename); 00094 00095 // Add the path to the file name 00096 std::string file_path = this->directory_ + filename; 00097 00098 // Create the new file descriptor, and add it to the vector of files 00099 FILE *new_file = fopen(file_path.c_str(), "w"); 00100 this->files_.push_back(new_file); 00101 00102 if (new_file) 00103 this->warn("Opened [%s] for logging.", file_path.c_str()); 00104 else 00105 this->error("Failed to open [%s] for logging.", file_path.c_str()); 00106 00107 return this->files_.size() - 1; 00108 } 00109 00110 void FileLogger::log(std::string filename, std::string output, ...) 00111 { 00112 // Grab the instance of the logger 00113 FileLogger* logger = FileLogger::getInstance(); 00114 00115 // Make sure the logger exists 00116 if (logger) 00117 { 00118 // Lock the mutex 00119 pthread_mutex_lock(&logger->write_mutex_); 00120 00121 // Make sure the filename exists 00122 if (filename.size() > 0) 00123 { 00124 int index = -1; 00125 00126 // Traverse the known filenames to see if we can find this filename 00127 for (unsigned c = 0; c < logger->filenames_.size(); c++) 00128 { 00129 // If the filename already exists exit 00130 if (logger->filenames_.at(c) == filename) 00131 { 00132 // Set the index to this filename 00133 index = c; 00134 break; 00135 } 00136 } 00137 00138 // Make sure the filename was found 00139 if (index > -1) 00140 { 00141 // Grab the file descriptor from the vector 00142 FILE *new_file = logger->files_.at(index); 00143 00144 // Make sure the file descriptor exists, write to the file 00145 if (new_file) 00146 { 00147 // Grab the list of variable arguments 00148 va_list ap; 00149 00150 // Print the entire list of variable arguments to the file 00151 va_start(ap, output); 00152 vfprintf(new_file, output.c_str(), ap); 00153 va_end(ap); 00154 00155 // Flush the output for the file 00156 fflush(new_file); 00157 } 00158 } 00159 else 00160 { 00161 // Create a new file, and get the index 00162 int idx = logger->openFile(filename); 00163 00164 // Make sure the index of the new file is valid 00165 if (idx >= 0 && idx < (int)logger->files_.size()) 00166 { 00167 // Grab the file descriptor of the file 00168 FILE *new_file = logger->files_.at(idx); 00169 00170 // Make sure the file descriptor exists, write to the file 00171 if (new_file) 00172 { 00173 // Grab the list of variable arguments 00174 va_list ap; 00175 00176 // Print the entire list of variable arguments to the file 00177 va_start(ap, output); 00178 vfprintf(new_file, output.c_str(), ap); 00179 va_end(ap); 00180 00181 // Flush the output for the file 00182 fflush(new_file); 00183 } 00184 } 00185 } 00186 } 00187 00188 // Unlock the mutex 00189 pthread_mutex_unlock(&logger->write_mutex_); 00190 } 00191 } 00192 |