1 /* 2 *Copyright (C) 2018 Laurent Tréguier 3 * 4 *This file is part of DLS. 5 * 6 *DLS is free software: you can redistribute it and/or modify 7 *it under the terms of the GNU General Public License as published by 8 *the Free Software Foundation, either version 3 of the License, or 9 *(at your option) any later version. 10 * 11 *DLS is distributed in the hope that it will be useful, 12 *but WITHOUT ANY WARRANTY; without even the implied warranty of 13 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 *GNU General Public License for more details. 15 * 16 *You should have received a copy of the GNU General Public License 17 *along with DLS. If not, see <http://www.gnu.org/licenses/>. 18 * 19 */ 20 21 module dls.util.logger; 22 23 import dls.protocol.interfaces : InitializeParams; 24 25 shared auto logger = new shared LspLogger(); 26 private immutable int[InitializeParams.Trace] traceToType; 27 28 shared static this() 29 { 30 import dls.protocol.interfaces : MessageType; 31 import std.conv : to; 32 import std.experimental.logger : LogLevel, globalLogLevel; 33 34 globalLogLevel = LogLevel.off; 35 36 //dfmt off 37 traceToType = [ 38 InitializeParams.Trace.off: 0, 39 InitializeParams.Trace.messages: MessageType.warning.to!int, 40 InitializeParams.Trace.verbose: MessageType.log.to!int 41 ]; 42 //dfmt on 43 } 44 45 private shared class LspLogger 46 { 47 import dls.protocol.interfaces : MessageType; 48 49 private int _messageType; 50 51 @property void trace(in InitializeParams.Trace t) 52 { 53 _messageType = traceToType[t]; 54 } 55 56 void info(in string message) const 57 { 58 sendMessage(message, MessageType.info); 59 } 60 61 void infof(Args...)(in string message, in Args args) const 62 { 63 import std.format : format; 64 65 info(format(message, args)); 66 } 67 68 void warning(in string message) const 69 { 70 sendMessage(message, MessageType.warning); 71 } 72 73 void warningf(Args...)(in string message, in Args args) const 74 { 75 import std.format : format; 76 77 warning(format(message, args)); 78 } 79 80 void error(in string message) const 81 { 82 sendMessage(message, MessageType.error); 83 } 84 85 void errorf(Args...)(in string message, in Args args) const 86 { 87 import std.format : format; 88 89 error(format(message, args)); 90 } 91 92 private void sendMessage(in string message, in MessageType type) const 93 { 94 import dls.protocol.interfaces : LogMessageParams; 95 import dls.protocol.jsonrpc : send; 96 import dls.protocol.messages.methods : Window; 97 import std.datetime : Clock; 98 import std.format : format; 99 100 if (type <= _messageType) 101 { 102 send(Window.logMessage, new LogMessageParams(type, 103 format!"[%.24s] %s"(Clock.currTime.toString(), message))); 104 } 105 } 106 }