Home | History | Annotate | Download | only in interface
      1 /*
      2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 // This singleton can be used for logging data for offline processing. Data
     12 // logged with it can conveniently be parsed and processed with e.g. Matlab.
     13 //
     14 // Following is an example of the log file format, starting with the header
     15 // row at line 1, and the data rows following.
     16 // col1,col2,col3,multi-value-col4[3],,,col5
     17 // 123,10.2,-243,1,2,3,100
     18 // 241,12.3,233,1,2,3,200
     19 // 13,16.4,-13,1,2,3,300
     20 //
     21 // As can be seen in the example, a multi-value-column is specified with the
     22 // name followed the number of elements it contains. This followed by
     23 // number of elements - 1 empty columns.
     24 //
     25 // Without multi-value-columns this format can be natively by Matlab. With
     26 // multi-value-columns a small Matlab script is needed, available at
     27 // trunk/tools/matlab/parseLog.m.
     28 //
     29 // Table names and column names are case sensitive.
     30 
     31 #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_DATA_LOG_H_
     32 #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_DATA_LOG_H_
     33 
     34 #include <string>
     35 
     36 #include "webrtc/system_wrappers/interface/data_log_impl.h"
     37 
     38 namespace webrtc {
     39 
     40 class DataLog {
     41  public:
     42   // Creates a log which uses a separate thread (referred to as the file
     43   // writer thread) for writing log rows to file.
     44   //
     45   // Calls to this function after the log object has been created will only
     46   // increment the reference counter.
     47   static int CreateLog();
     48 
     49   // Decrements the reference counter and deletes the log when the counter
     50   // reaches 0. Should be called equal number of times as successful calls to
     51   // CreateLog or memory leak will occur.
     52   static void ReturnLog();
     53 
     54   // Combines the string table_name and the integer table_id into a new string
     55   // table_name + _ + table_id. The new string will be lower-case.
     56   static std::string Combine(const std::string& table_name, int table_id);
     57 
     58   // Adds a new table, with the name table_name, and creates the file, with the
     59   // name table_name + ".txt", to which the table will be written.
     60   // table_name is treated in a case sensitive way.
     61   static int AddTable(const std::string& table_name);
     62 
     63   // Adds a new column to a table. The column will be a multi-value-column
     64   // if multi_value_length is greater than 1.
     65   // table_name and column_name are treated in a case sensitive way.
     66   static int AddColumn(const std::string& table_name,
     67                        const std::string& column_name,
     68                        int multi_value_length);
     69 
     70   // Inserts a single value into a table with name table_name at the column with
     71   // name column_name.
     72   // Note that the ValueContainer makes use of the copy constructor,
     73   // operator= and operator<< of the type T, and that the template type must
     74   // implement a deep copy copy constructor and operator=.
     75   // Copy constructor and operator= must not be disabled for the type T.
     76   // table_name and column_name are treated in a case sensitive way.
     77   template<class T>
     78   static int InsertCell(const std::string& table_name,
     79                         const std::string& column_name,
     80                         T value) {
     81     DataLogImpl* data_log = DataLogImpl::StaticInstance();
     82     if (data_log == NULL)
     83       return -1;
     84     return data_log->InsertCell(
     85              table_name,
     86              column_name,
     87              new ValueContainer<T>(value));
     88   }
     89 
     90   // Inserts an array of values into a table with name table_name at the
     91   // column specified by column_name, which must be a multi-value-column.
     92   // Note that the MultiValueContainer makes use of the copy constructor,
     93   // operator= and operator<< of the type T, and that the template type
     94   // must implement a deep copy copy constructor and operator=.
     95   // Copy constructor and operator= must not be disabled for the type T.
     96   // table_name and column_name are treated in a case sensitive way.
     97   template<class T>
     98   static int InsertCell(const std::string& table_name,
     99                         const std::string& column_name,
    100                         const T* array,
    101                         int length) {
    102     DataLogImpl* data_log = DataLogImpl::StaticInstance();
    103     if (data_log == NULL)
    104       return -1;
    105     return data_log->InsertCell(
    106              table_name,
    107              column_name,
    108              new MultiValueContainer<T>(array, length));
    109   }
    110 
    111   // For the table with name table_name: Writes the current row to file.
    112   // Starts a new empty row.
    113   // table_name is treated in a case-sensitive way.
    114   static int NextRow(const std::string& table_name);
    115 };
    116 
    117 }  // namespace webrtc
    118 
    119 #endif  // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_DATA_LOG_H_
    120