由于需要将平时做实验时程序的一些输出信息保存下来,以前都是截图之类的,这样虽然也能将输出信息保存下来,但是这样不方便后面整理实验数据,一个比较好的方法是直接将输出信息保存为文本的格式,这样既能保存数据,也方便以后整理数据。
要将输出信息保存下来,有两个问题要解决:
1.文件保存的名字;
2.如何将不同类型的数据输出到文件中。
对于第一个问题,以及在另外一个博客已经解决了,现在来解决第二个问题,在OpenCV中,已经重载过输出操作符<<了,因此,在基于OpenCV的情况下,我们只需要定义一个代理类,在代理类中重载输出操作符,然后就可以将代理类按照普通的输出流来使用了。
之所以使用代理类,是为了能够在产生日志文件的时候,能够自动命名为当前时间,同时,也能够自动关闭文件。
代理类主要实现构造函数,析构函数和重载输出操作符,将变量输出到文件中主要是有代理类的fstream成员实现的。
具体的代码如下:
logging.h
/*M/////////////////////////////////////////////////////////////////////////// // Copyright (c) 2014, sheng // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // //M*/ #ifndef LOGGING_H #define LOGGING_H #include <iostream> #include <fstream> #include <string> class Logging { public: Logging(); Logging(const std::string& FileName); ~Logging(); template <class T> Logging &operator <<(const T &Input); private: std::fstream* ActualFile; std::string GetCurrentDate(); }; /** * @brief Logging::operator << The template output operator which is used to * output the Input to the file. * @param Input The variable which should be saved in the log file. * * @author sheng * @date 2014-08-22 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-08-22 0.1 build the function * */ template <class T> Logging& Logging::operator <<(const T& Input) { if (ActualFile) { // write the Input to the log file. (*ActualFile) << Input; } return *this; } #endif // LOGGING_H
logging.cpp
/*M/////////////////////////////////////////////////////////////////////////// // Copyright (c) 2014, sheng // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // //M*/ #include "logging.h" #include "CurrentTime.h" /** * @brief Logging::Logging The default constructor * * @author sheng * @date 2014-08-21 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-08-21 0.1 build the function * */ Logging::Logging() : ActualFile(NULL) { // get the current date std::string FileName = GetCurrentDate(); // construct a fstream ActualFile = new std::fstream; // open the output file ActualFile->open(FileName.c_str(), std::ios_base::out); } /** * @brief Logging::Logging The overload constructor * @param FileName The prefix of the actural file name * * @author sheng * @date 2014-08-21 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-08-21 0.1 build the function * */ Logging::Logging(const std::string &FileName) : ActualFile(NULL) { // consturct a fstream ActualFile = new std::fstream; // Acturall file name std::string ActuralFileName = FileName + GetCurrentDate(); // open the output file ActualFile->open(ActuralFileName.c_str(), std::ios_base::out); } /** * @brief Logging::~Logging The destructor * * @author sheng * @date 2014-08-21 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-08-21 0.1 build the function * */ Logging::~Logging() { if (ActualFile) { // closed the file ActualFile->close(); // release the memory delete ActualFile; } } /** * @brief Logging::GetCurrentDate Get a string which represent the local current * time. * @return The local current time in the type of string. * * @author sheng * @date 2014-08-21 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-08-21 0.1 build the function * */ std::string Logging::GetCurrentDate() { // get the current date in string std::string Result = GetCurrentTime(); return Result; }
Test_Logging.cpp
/*M/////////////////////////////////////////////////////////////////////////// // Copyright (c) 2014, sheng // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // //M*/ #include "logging.h" #include "opencv2/opencv.hpp" /** * @brief Test_Loggng The unit test for the logging class. * * @author sheng * @date 2014-08-21 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-08-21 0.1 build the function * */ void Test_Loggng() { Logging Logger; // test int int i = 2; Logger << i << '\n'; // test char char a = 'a'; Logger << a << '\n'; // test long long lv = 1234567890; Logger << lv << '\n'; // test char array char* b = "adede"; Logger << b << '\n'; // test string std::string strv = "string"; Logger << strv << '\n'; //test cv::Mat cv::Mat Image(2,2, CV_8UC1, cv::Scalar(10)); Logger << Image << '\n'; // prefix std::string FileName = "Hello"; Logging LoggerInName(FileName); LoggerInName << i << "\n"; LoggerInName << a << "\n"; LoggerInName << b << "\n"; LoggerInName << strv << "\n"; LoggerInName << Image << "\n"; }
github: https://github.com/shengno/Logging