/
TraceConfigStorage.h
178 lines (151 loc) · 3.63 KB
/
TraceConfigStorage.h
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
/*
* PROGRAM: Firebird Trace Services
* MODULE: TraceConfigStorage.h
* DESCRIPTION: Trace API shared configurations storage
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Khorsun Vladyslav
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2008 Khorsun Vladyslav <hvlad@users.sourceforge.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*/
#ifndef JRD_TRACECONFIGSTORAGE_H
#define JRD_TRACECONFIGSTORAGE_H
#include "../../common/classes/array.h"
#include "../../common/classes/fb_string.h"
#include "../../common/classes/init.h"
#include "../../common/isc_s_proto.h"
#include "../../jrd/trace/TraceSession.h"
#include "../../common/classes/RefCounted.h"
namespace Jrd {
struct TraceCSHeader : public MemoryHeader
{
volatile ULONG change_number;
volatile ULONG session_number;
ULONG cnt_uses;
char cfg_file_name[MAXPATHLEN];
};
class ConfigStorage : public Firebird::GlobalStorage, public SharedMemory<TraceCSHeader>
{
public:
ConfigStorage();
~ConfigStorage();
void addSession(Firebird::TraceSession& session);
bool getNextSession(Firebird::TraceSession& session);
void removeSession(ULONG id);
void restart();
void updateSession(Firebird::TraceSession& session);
ULONG getChangeNumber() const
{ return sh_mem_header ? sh_mem_header->change_number : 0; }
void acquire();
void release();
private:
void mutexBug(int osErrorCode, const char* text);
bool initialize(bool);
void checkFile();
void touchFile();
class TouchFile : public Firebird::RefCntIface<Firebird::ITimer, FB_TIMER_VERSION>
{
public:
void FB_CARG handler();
void start(const char* fName);
void stop();
int FB_CARG release();
private:
const char* fileName;
};
Firebird::RefPtr<TouchFile> timer;
void checkDirty()
{
if (m_dirty)
{
//_commit(m_cfg_file);
m_dirty = false;
}
}
void setDirty()
{
if (!m_dirty)
{
sh_mem_header->change_number++;
m_dirty = true;
}
}
// items in every session record at sessions file
enum ITEM
{
tagID = 1, // session ID
tagName, // session Name
tagUserName, // creator user name
tagFlags, // session flags
tagConfig, // configuration
tagStartTS, // date+time when started
tagLogFile, // log file name, if any
tagEnd
};
void putItem(ITEM tag, ULONG len, const void* data);
bool getItemLength(ITEM& tag, ULONG& len);
int m_recursive;
int m_cfg_file;
bool m_dirty;
};
class StorageInstance : private Firebird::InstanceControl
{
private:
Firebird::Mutex initMtx;
ConfigStorage* storage;
public:
void dtor()
{
delete storage;
storage = 0;
}
StorageInstance() :
storage(NULL)
{}
ConfigStorage* getStorage()
{
if (!storage)
{
Firebird::MutexLockGuard guard(initMtx);
if (!storage)
{
storage = new ConfigStorage;
new Firebird::InstanceControl::InstanceLink<StorageInstance>(this);
}
}
return storage;
}
};
class StorageGuard
{
public:
explicit StorageGuard(ConfigStorage* storage) :
m_storage(storage)
{
m_storage->acquire();
}
~StorageGuard()
{
m_storage->release();
}
private:
ConfigStorage* m_storage;
};
}
#endif