LSP: Adds configuration option to enable trace logging to a local file.

This commit is contained in:
Christian Parpart 2022-09-01 17:30:52 +02:00
parent f808855329
commit 4258f58323
5 changed files with 42 additions and 1 deletions

View File

@ -19,6 +19,7 @@ Compiler Features:
* Code Generator: More efficient overflow checks for multiplication.
* Language Server: Analyze all files in a project by default (can be customized by setting ``'file-load-strategy'`` to ``'directly-opened-and-on-import'`` in LSP settings object).
* Yul Optimizer: Simplify the starting offset of zero-length operations to zero.
* Language Server: Adds configuration option ``trace-log-file`` to enable trace logging to a local file.
Bugfixes:

View File

@ -186,6 +186,16 @@ void LanguageServer::changeConfiguration(Json::Value const& _settings)
lspAssert(false, ErrorCode::InvalidParams, "Invalid file load strategy: " + text);
}
if (auto traceLogFileNode = _settings["trace-log-file"])
{
if (traceLogFileNode.isString())
m_client.setTraceLogFile(boost::filesystem::path(_settings["trace-log-file"].asString()));
if (traceLogFileNode.isNull())
m_client.setTraceLogFile(nullopt);
else
lspAssert(false, ErrorCode::InvalidParams, "Invalid trace-log-file value. Must be a path to a file.");
}
m_settingsObject = _settings;
Json::Value jsonIncludePaths = _settings["include-paths"];

View File

@ -27,6 +27,7 @@
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
@ -58,6 +59,12 @@ optional<Json::Value> Transport::receive()
string const data = readBytes(stoul(headers->at("content-length")));
if (m_traceLogFilePath)
{
ofstream traceLogger(m_traceLogFilePath.value().generic_string(), ios::app);
traceLogger << "Received: " << data << endl;
}
Json::Value jsonMessage;
string jsonParsingErrors;
solidity::util::jsonParseStrict(data, jsonMessage, &jsonParsingErrors);
@ -70,7 +77,7 @@ optional<Json::Value> Transport::receive()
return {std::move(jsonMessage)};
}
void Transport::trace(std::string _message, Json::Value _extra)
void Transport::trace(string _message, Json::Value _extra)
{
if (m_logTrace != TraceValue::Off)
{
@ -82,6 +89,11 @@ void Transport::trace(std::string _message, Json::Value _extra)
}
}
void Transport::setTraceLogFile(std::optional<boost::filesystem::path> _pathToLogFile)
{
m_traceLogFilePath = std::move(_pathToLogFile);
}
optional<map<string, string>> Transport::parseHeaders()
{
map<string, string> headers;
@ -137,6 +149,12 @@ void Transport::send(Json::Value _json, MessageID _id)
// Trailing CRLF only for easier readability.
string const jsonString = solidity::util::jsonCompactPrint(_json);
if (m_traceLogFilePath)
{
ofstream traceLogger(m_traceLogFilePath.value().generic_string(), ios::app);
traceLogger << "Sending: " << jsonString << endl;
}
writeBytes(fmt::format("Content-Length: {}\r\n\r\n", jsonString.size()));
writeBytes(jsonString);
flushOutput();

View File

@ -21,6 +21,8 @@
#include <json/value.h>
#include <boost/filesystem/path.hpp>
#include <functional>
#include <iosfwd>
#include <map>
@ -103,8 +105,13 @@ public:
TraceValue traceValue() const noexcept { return m_logTrace; }
void setTrace(TraceValue _value) noexcept { m_logTrace = _value; }
/// Sets path to a local log file (in addition to reporting the trace log to the client)
/// to be written to if trace value is set to verbose.
void setTraceLogFile(std::optional<boost::filesystem::path> _pathToLogfile);
private:
TraceValue m_logTrace = TraceValue::Off;
std::optional<boost::filesystem::path> m_traceLogFilePath;
protected:
/// Reads from the transport and parses the headers until the beginning

View File

@ -948,6 +948,11 @@ class SolidityLSPTestSuite: # {{{
params['initializationOptions'] = {}
params['initializationOptions']['include-paths'] = custom_include_paths
if self.trace_io:
if params['initializationOptions'] is None:
params['initializationOptions'] = {}
params['initializationOptions']['trace-log-file'] = "solc.log"
if not expose_project_root:
params['rootUri'] = None