1 /** 2 Copyright: Copyright (c) 2016-2017 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 module voxelman.log.binlog; 7 8 import core.sync.mutex; 9 import std.stdio : File; 10 11 import voxelman.utils.filewriter; 12 import cbor; 13 14 void binlog(T...)(T args) { 15 logger.log(args); 16 } 17 18 void initBinLog(string filename) { 19 logger.open(filename); 20 } 21 22 void closeBinLog() { 23 logger.close(); 24 } 25 26 private __gshared BinLogger logger; 27 28 private struct BinLogger 29 { 30 Mutex mutex; 31 File file; 32 FileWriter writer; 33 34 void open(string filename) 35 { 36 file.open(filename, "wb+"); 37 writer = FileWriter(file); 38 mutex = new Mutex; 39 } 40 41 void log(T...)(T args) 42 { 43 synchronized(mutex) 44 { 45 encodeCborArrayHeader(writer, args.length); 46 foreach(arg; args) 47 { 48 encodeCbor(writer, arg); 49 } 50 } 51 } 52 53 void close() 54 { 55 synchronized(mutex) 56 { 57 writer.flush(); 58 file.close(); 59 } 60 destroy(mutex); 61 } 62 }