• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
Logger()21   Logger() : enabled_(false) {}
~Logger()22   ~Logger() {
23     if (enabled_) logfile_.close();
24   }
25 
Enable(std::string path)26   static void Enable(std::string path) {
27     Get().enabled_ = true;
28     Get().logfile_.open(path);
29   }
30 
31   template <class... Args>
Log(Args &&...args)32   static void Log(Args&&... args) {
33     if (Enabled()) {
34       USE((Stream() << std::forward<Args>(args))...);
35       Flush();
36     }
37   }
38 
39  private:
Enabled()40   static bool Enabled() { return Get().enabled_; }
Stream()41   static std::ofstream& Stream() {
42     CHECK(Get().enabled_);
43     return Get().logfile_;
44   }
Flush()45   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_
59