File: messaginghub.cpp

package info (click to toggle)
asc 2.4.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 75,240 kB
  • sloc: cpp: 155,036; sh: 8,829; ansic: 6,890; makefile: 652; perl: 138
file content (188 lines) | stat: -rw-r--r-- 4,782 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


 #include "messaginghub.h"

 
StatusMessageWindowHolder :: StatusMessageWindowHolder( const StatusMessageWindowHolder& smw )
   : userData( NULL )
{
   copy(smw);
}
 
StatusMessageWindowHolder::StatusMessageWindowHolder()
{
   userData = new UserData;
}


void StatusMessageWindowHolder::unlink()
{
   if ( userData ) {
      userData->counter -= 1;
      if ( userData->counter <= 0 ) {
         delete userData;
         userData = NULL;
      }
   }
}

void StatusMessageWindowHolder::copy( const StatusMessageWindowHolder& smw )
{
   if ( smw.userData ) {
      unlink();
      userData = smw.userData;
      userData->counter += 1;
   } else {
      userData = new UserData;
   }   
}

void StatusMessageWindowHolder::close()
{
   unlink();
}   


StatusMessageWindowHolder& StatusMessageWindowHolder::operator=( const StatusMessageWindowHolder& smw )
{
   copy(smw);
   return *this;
}


StatusMessageWindowHolder::~StatusMessageWindowHolder()
{
   unlink();
}

StatusMessageWindowHolder MessagingHubBase::infoMessageWindow( const ASCString& msg ) { 
   return StatusMessageWindowHolder( messageWindowFactory( msg )); 
}


void MessagingHubBase::message( MessageType type, const ASCString& message )
{
   switch ( type ) {
      case FatalError: fatalError( message ); 
                       exitHandler();
                       break;
      case Error:      error ( message ); break;
      case Warning:    warning( message ); break;
      case InfoMessage: infoMessage( message ); break;
      case StatusInfo: statusInformation( message ); break;
      case LogMessage: logMessage( message, 0 ); break;
   };   

}

void MessagingHubBase::setLoggingCategory( const ASCString& category, bool enable )
{
   if ( enable ) {
      if ( enabledLogCategories.find( category ) == enabledLogCategories.end())
         enabledLogCategories.insert( category );
   } else {
      if ( enabledLogCategories.find( category ) != enabledLogCategories.end())
         enabledLogCategories.erase( category );
   }
}

 
void MessagingHubBase::message( MessageType type, const char* msg, ... )
{
   va_list arglist;
   va_start ( arglist, msg );

   ASCString my_message;
   my_message.vaformat( msg, arglist );
   
   message( type, my_message );  
   
   va_end ( arglist );
}
 
  
void displayLogMessage ( int msgVerbosity, const char* message, ... )
{
   va_list arglist;
   va_start ( arglist, message );
   if ( msgVerbosity <= MessagingHub::Instance().getVerbosity() ) {
      char buf[10000];
      vsprintf ( buf, message, arglist );

      displayLogMessage( msgVerbosity, ASCString(buf) );
   }
   va_end ( arglist );
}

void displayLogMessage ( int msgVerbosity, const ASCString& message )
{
   if ( msgVerbosity <= MessagingHub::Instance().getVerbosity() ) 
      MessagingHub::Instance().logMessage( message, msgVerbosity );

}


bool MessagingHubBase::logCategoryEnabled(const ASCString& category)
{
   return enabledLogCategories.find( category ) != enabledLogCategories.end();
}

void logMessage ( const ASCString& category, const ASCString& message )
{
   if ( MessagingHub::Instance().logCategoryEnabled( category ) )
      MessagingHub::Instance().logCategorizedMessage( category, message );
}

void fatalError ( const ASCString& string )
{
   MessagingHub::Instance().message( MessagingHubBase::FatalError, string );
}

void fatalError ( const char* msg, ... )
{
   va_list arglist;
   va_start ( arglist, msg );

   ASCString message;
   message.vaformat( msg, arglist );

   fatalError( message );
      
   va_end ( arglist );
}


void warning ( const ASCString& str )
{
   MessagingHub::Instance().message( MessagingHubBase::Warning, str );
}

void warningMessage ( const ASCString& str )
{
   MessagingHub::Instance().message( MessagingHubBase::Warning, str );
}

void errorMessage ( const ASCString& string )
{
   MessagingHub::Instance().message( MessagingHubBase::Error, string );
}

void infoMessage ( const ASCString& string )
{
   MessagingHub::Instance().message( MessagingHubBase::InfoMessage, string );
}

void statusMessage ( const ASCString& string )
{
   MessagingHub::Instance().message( MessagingHubBase::StatusInfo, string );
}