1 /* -----------------------------------------------------------------------------
2 The copyright in this software is being made available under the BSD
3 License, included below. No patent rights, trademark rights and/or
4 other Intellectual Property Rights other than the copyrights concerning
5 the Software are granted under this license.
6 
7 For any license concerning other Intellectual Property rights than the software,
8 especially patent licenses, a separate Agreement needs to be closed.
9 For more information please contact:
10 
11 Fraunhofer Heinrich Hertz Institute
12 Einsteinufer 37
13 10587 Berlin, Germany
14 www.hhi.fraunhofer.de/vvc
15 vvc@hhi.fraunhofer.de
16 
17 Copyright (c) 2018-2021, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
18 All rights reserved.
19 
20 Redistribution and use in source and binary forms, with or without
21 modification, are permitted provided that the following conditions are met:
22 
23  * Redistributions of source code must retain the above copyright notice,
24    this list of conditions and the following disclaimer.
25  * Redistributions in binary form must reproduce the above copyright notice,
26    this list of conditions and the following disclaimer in the documentation
27    and/or other materials provided with the distribution.
28  * Neither the name of Fraunhofer nor the names of its contributors may
29    be used to endorse or promote products derived from this software without
30    specific prior written permission.
31 
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
36 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
42 THE POSSIBILITY OF SUCH DAMAGE.
43 
44 
45 ------------------------------------------------------------------------------------------- */
46 
47 /** \file     dtrace.h
48  *  \brief    Implementation of trace messages support for debugging
49  */
50 
51 #pragma once
52 
53 #include <stdio.h>
54 
55 #include <string>
56 #include <list>
57 #include <map>
58 #include <set>
59 #include <vector>
60 #include <cstdarg>
61 
62 namespace vvdec
63 {
64 
65 class CDTrace;
66 
67 typedef std::string CType;
68 
69 struct dtrace_channel
70 {
71   int channel_number;
72   std::string channel_name;
73 };
74 
75 typedef std::vector<dtrace_channel> dtrace_channels_t;
76 
77 class Condition
78 {
79 public:
80     CType type;
81     bool ( *eval )( int, int );
82     int rval;
83 
Condition(CType t,bool (* efunc)(int,int),int refval)84     Condition( CType t, bool ( *efunc )( int,int ), int refval )
85     : type(t), eval(efunc), rval(refval)
86     {}
87 };
88 
89 class Channel
90 {
91     typedef std::vector<Condition> Rule;
92 public:
Channel()93     Channel() : rule_list(), _active(false), _counter(0) {}
94     void update( std::map< CType, int > state );
active()95     bool active() { return _active; }
96     void add( Rule rule );
incrementCounter()97     void incrementCounter() { _counter++; }
decrementCounter()98     void decrementCounter() { _counter--  ; }
getCounter()99     int64_t getCounter() { return _counter; }
100 private:
101     std::list< Rule > rule_list;
102     bool _active;
103     int64_t _counter;
104 };
105 
106 class CDTrace
107 {
108   typedef std::pair< CType, int > state_type;
109   //friend class Rules;
110 private:
111     bool          copy;
112     FILE         *m_trace_file;
113     int           m_error_code;
114 
115     typedef std::string Key;
116     typedef std::vector<std::string> vstring;
117     typedef std::map< Key, int > channel_map_t;
118     std::vector< Channel > chanRules;
119     std::set< CType > condition_types;
120     std::map< CType, int > state;
121     std::map< Key, int > deserializationTable;
122 
123 public:
CDTrace()124     CDTrace() : copy(false), m_trace_file(NULL) {}
125     CDTrace( const char *filename, vstring channel_names );
126     CDTrace( const char *filename, const dtrace_channels_t& channels );
127     CDTrace( const std::string& sTracingFile, const std::string& sTracingRule, const dtrace_channels_t& channels );
128     CDTrace( const CDTrace& other );
129     CDTrace& operator=( const CDTrace& other );
130     ~CDTrace();
131     void swap         ( CDTrace& other );
132     int  addRule      ( std::string rulestring );
133     template<bool bCount>
134     void dtrace       ( int, const char *format, /*va_list args*/... );
135     void dtrace_repeat( int, int i_times, const char *format, /*va_list args*/... );
136     bool update       ( state_type stateval );
137     int  init( vstring channel_names );
getLastError()138     int  getLastError() { return m_error_code;  }
139     const char*  getChannelName( int channel_number );
140     void getChannelsList( std::string& sChannels );
141     std::string getErrMessage();
getChannelCounter(int channel)142     int64_t getChannelCounter( int channel ) { return chanRules[channel].getCounter(); }
decrementChannelCounter(int channel)143     void    decrementChannelCounter( int channel ) { chanRules[channel].decrementCounter(); }
144 };
145 
146 }
147