Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LogWriteHandler.cs « Server « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec661657ae14a5dce0d0ff770c0376cd4daffd9f (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//  Copyright (C) 2015, The Duplicati Team

//  http://www.duplicati.com, info@duplicati.com
//
//  This library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of the
//  License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Linq;
using Duplicati.Library.Logging;
using System.Collections.Generic;

namespace Duplicati.Server
{
    /// <summary>
    /// Class that handles logging from the server, 
    /// and provides an entry point for the runner
    /// to redirect log output to a file
    /// </summary>
    public class LogWriteHandler : ILogDestination, IDisposable
    {
        /// <summary>
        /// The number of messages to keep when inactive
        /// </summary>
        private const int INACTIVE_SIZE = 30;
        /// <summary>
        /// The number of messages to keep when active
        /// </summary>
        private const int ACTIVE_SIZE = 5000;

        /// <summary>
        /// The context key used for conveying the backup ID
        /// </summary>
        public const string LOG_EXTRA_BACKUPID = "BackupID";
        /// <summary>
        /// The context key used for conveying the task ID
        /// </summary>
        public const string LOG_EXTRA_TASKID = "TaskID";

        /// <summary>
        /// Represents a single log event
        /// </summary>
        public struct LogEntry
        {
            /// <summary>
            /// A unique ID that sequentially increments
            /// </summary>
            private static long _id;

            /// <summary>
            /// The time the message was logged
            /// </summary>
            public readonly DateTime When;

            /// <summary>
            /// The ID assigned to the message
            /// </summary>
            public readonly long ID;

            /// <summary>
            /// The logged message
            /// </summary>
            public readonly string Message;

            /// <summary>
            /// The log tag
            /// </summary>
            public readonly string Tag;

            /// <summary>
            /// The message ID
            /// </summary>
            public readonly string MessageID;

            /// <summary>
            /// The message ID
            /// </summary>
            public readonly string ExceptionID;

            /// <summary>
            /// The message type
            /// </summary>
            public readonly LogMessageType Type;

            /// <summary>
            /// Exception data attached to the message
            /// </summary>
            public readonly Exception Exception;

            /// <summary>
            /// The backup ID, if any
            /// </summary>
            public readonly string BackupID;

            /// <summary>
            /// The task ID, if any
            /// </summary>
            public readonly string TaskID;

            /// <summary>
            /// Initializes a new instance of the <see cref="Duplicati.Server.LogWriteHandler+LogEntry"/> struct.
            /// </summary>
            /// <param name="entry">The log entry to store</param>
            public LogEntry(Duplicati.Library.Logging.LogEntry entry)
            {
                this.ID = System.Threading.Interlocked.Increment(ref _id);
                this.When = entry.When;
                this.Message = entry.FormattedMessage;
                this.Type = entry.Level;
                this.Exception = entry.Exception;
                this.Tag = entry.Tag;
                this.MessageID = entry.Id;
                this.BackupID = entry[LOG_EXTRA_BACKUPID];
                this.TaskID = entry[LOG_EXTRA_TASKID];

                if (entry.Exception == null)
                    this.ExceptionID = null;
                else if (entry.Exception is Library.Interface.UserInformationException)
                    this.ExceptionID = ((Library.Interface.UserInformationException)entry.Exception).HelpID;
                else
                    this.ExceptionID = entry.Exception.GetType().FullName;
                    
            }
        }

        /// <summary>
        /// Basic implementation of a ring-buffer
        /// </summary>
        private class RingBuffer<T> : IEnumerable<T>
        {
            private T[] m_buffer;
            private int m_head;
            private int m_tail;
            private int m_length;
            private int m_key;
            private object m_lock = new object();

            public RingBuffer(int size, IEnumerable<T> initial = null)
            {
                m_buffer = new T[size];
                if (initial != null)
                    foreach(var t in initial)
                        this.Enqueue(t);
            }
                
            public int Length { get { return m_length; } }

            public T Dequeue()
            {
                lock(m_lock)
                {
                    if (m_length == 0)
                        throw new ArgumentOutOfRangeException(nameof(m_length), "Buffer is empty");
                    
                    m_key++;
                    var ix = m_tail;
                    m_tail = (m_tail + 1) % m_buffer.Length;
                    m_length--;

                    return m_buffer[ix];
                }
            }

            public void Enqueue(T item)
            {
                lock(m_lock)
                {
                    m_key++;
                    m_buffer[m_head] = item;
                    m_head = (m_head + 1) % m_buffer.Length;
                    if (m_length == m_buffer.Length)
                        m_tail = (m_tail + 1) % m_buffer.Length;
                    else
                        m_length++;
                }
            }

            #region IEnumerable implementation
            public IEnumerator<T> GetEnumerator()
            {
                var k = m_key;
                for(var i = 0; i < m_length; i++)
                    if (m_key != k)
                        throw new InvalidOperationException("Buffer was modified while reading");
                    else
                        yield return m_buffer[(m_tail + i) % m_buffer.Length];
            }
            #endregion
            #region IEnumerable implementation
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
            #endregion

            public T[] FlatArray(Func<T, bool> filter = null)
            {
                lock(m_lock)
                    if (filter == null)
                        return this.ToArray();
                    else
                        return this.Where(filter).ToArray();
            }

            public void Clear()
            {
                lock(m_lock)
                {
                    m_length = 0;
                    m_tail = 0;
                    m_head = 0;
                    m_buffer = new T[m_buffer.Length];
                }
            }

            public int Size { get { return m_buffer.Length; } }
        }

        private DateTime[] m_timeouts;
        private object m_lock = new object();
        private volatile bool m_anytimeouts = false;
        private RingBuffer<LogEntry> m_buffer;

        private ILogDestination m_serverfile;
        private LogMessageType m_serverloglevel;
        private LogMessageType m_logLevel;

        public LogWriteHandler()
        {
            var fields = Enum.GetValues(typeof(LogMessageType));
            m_timeouts = new DateTime[fields.Length];
            m_buffer = new RingBuffer<LogEntry>(INACTIVE_SIZE);
        }

        public void RenewTimeout(LogMessageType type)
        {
            lock(m_lock)
            {
                m_timeouts[(int)type] = DateTime.Now.AddSeconds(30);
                m_anytimeouts = true;
                if (m_buffer == null || m_buffer.Size == INACTIVE_SIZE)
                    m_buffer = new RingBuffer<LogEntry>(ACTIVE_SIZE, m_buffer);
            }
        }

        public void SetServerFile(string path, LogMessageType level)
        {
            var dir = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(path));
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);

            m_serverfile = new StreamLogDestination(path);
            m_serverloglevel = level;

            UpdateLogLevel();
        }

        public LogEntry[] AfterTime(DateTime offset, LogMessageType level)
        {
            RenewTimeout(level);
            UpdateLogLevel();

            offset = offset.ToUniversalTime();
            lock(m_lock)
            {
                if (m_buffer == null)
                    return new LogEntry[0];
                
                return m_buffer.FlatArray((x) => x.When > offset && x.Type >= level );
            }
        }

        public LogEntry[] AfterID(long id, LogMessageType level, int pagesize)
        {
            RenewTimeout(level);
            UpdateLogLevel();

            lock(m_lock)
            {
                if (m_buffer == null)
                    return new LogEntry[0];
                
                var buffer = m_buffer.FlatArray((x) => x.ID > id && x.Type >= level );
                // Return the <page_size> newest entries
                if (buffer.Length > pagesize) {
                    var index = buffer.Length - pagesize;
                    return buffer.Skip(index).Take(pagesize).ToArray();
                }
                else {
                    return buffer;
                }
            }
        }

        private int[] GetActiveTimeouts()
        {
            var i = 0;
            return (from n in m_timeouts
                                let ix = i++
                                where n > DateTime.Now
                                select ix).ToArray();
        }

        private void UpdateLogLevel()
        {   
            m_logLevel =
                (LogMessageType)(GetActiveTimeouts().Union(new int[] { (int)m_serverloglevel }).Min());
        }


        #region ILog implementation

        public void WriteMessage(Duplicati.Library.Logging.LogEntry entry)
        {
            if (entry.Level < m_logLevel) 
                return;
            
            if (m_serverfile != null && entry.Level >= m_serverloglevel)
                try
                {
                    m_serverfile.WriteMessage(entry);
                }
                catch
                {
                }

            lock(m_lock)
            {
                if (m_anytimeouts)
                {
                    var q = GetActiveTimeouts();

                    if (q.Length == 0)
                    {
                        UpdateLogLevel();
                        m_anytimeouts = false;
                        if (m_buffer == null || m_buffer.Size != INACTIVE_SIZE)
                            m_buffer = new RingBuffer<LogEntry>(INACTIVE_SIZE, m_buffer);

                    }
                }

                if (m_buffer != null)
                    m_buffer.Enqueue(new LogEntry(entry));
            }

        }

        #endregion

        #region IDisposable implementation

        public void Dispose()
        {
            if (m_serverfile != null)
            {
                var sf = m_serverfile;
                m_serverfile = null;
                if (sf is IDisposable)
                    ((IDisposable)sf).Dispose();
            }
        }

        #endregion
    }
}