Line data Source code
1 : // Copyright 2019 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_TORQUE_LS_GLOBALS_H_
6 : #define V8_TORQUE_LS_GLOBALS_H_
7 :
8 : #include <fstream>
9 : #include "src/torque/contextual.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 : namespace torque {
14 :
15 : // When the language server is run by VS code, stdout can not be seen, as it is
16 : // used as the communication channel. For debugging purposes a simple
17 : // Log class is added, that allows writing diagnostics to a file configurable
18 : // via command line flag.
19 : class Logger : public ContextualClass<Logger> {
20 : public:
21 : Logger() : enabled_(false) {}
22 : ~Logger() {
23 : if (enabled_) logfile_.close();
24 : }
25 :
26 : static void Enable(std::string path) {
27 : Get().enabled_ = true;
28 : Get().logfile_.open(path);
29 : }
30 :
31 : template <class... Args>
32 0 : static void Log(Args&&... args) {
33 0 : if (Enabled()) {
34 0 : USE((Stream() << std::forward<Args>(args))...);
35 : Flush();
36 : }
37 0 : }
38 :
39 : private:
40 0 : static bool Enabled() { return Get().enabled_; }
41 0 : static std::ofstream& Stream() {
42 0 : CHECK(Get().enabled_);
43 0 : return Get().logfile_;
44 : }
45 0 : static void Flush() { Get().logfile_.flush(); }
46 :
47 : private:
48 : bool enabled_;
49 : std::ofstream logfile_;
50 : };
51 :
52 : DECLARE_CONTEXTUAL_VARIABLE(TorqueFileList, std::vector<std::string>);
53 :
54 : } // namespace torque
55 : } // namespace internal
56 : } // namespace v8
57 :
58 : #endif // V8_TORQUE_LS_GLOBALS_H_
|