3

clangd writes log messages to stderr. In Linux, I need it to redirect stderr to a log file for future use, instead of console

the command I used is:

clangd --clang-tidy -compile-commands-dir=$SOME_PATH --log=error > $SOME_PATH/clangd.log 2>&1

It can start and redirect to the file, but after several seconds, clangd stops and has this error in the clangd.log file

I[18:10:31.484] clangd version 9.0.0 (https://github.com/llvm-mirror/llvm c62b24f070c9a4bb1a76409e623042a740cac4cd)
I[18:10:31.484] Working directory: ......
I[18:10:31.484] argv[0]: clangd
I[18:10:31.484] argv[1]: --clang-tidy
I[18:10:31.484] argv[2]: -compile-commands-dir=......
I[18:10:31.484] argv[3]: --log=error
I[18:10:31.484] Starting LSP over stdin/stdout
......

I[18:10:31.706] <-- initialize("1")
I[18:10:31.707] --> reply:initialize("1") 0 ms
Content-Length: 1038
......


V[18:10:41.710] <<< {"id":"2","jsonrpc":"2.0","method":"shutdown","params":null}

I[18:10:41.711] <-- shutdown("2")
I[18:10:41.711] --> reply:shutdown("2") 0 ms
Content-Length: 40

{"id":"2","jsonrpc":"2.0","result":null}V[18:10:41.711] >>> {"id":"2","jsonrpc":"2.0","result":null}

E[18:10:46.711] Transport error: Input/output error

What am I missing ? Why transport error ? to clangd process, it shouldn't be a problem whether the stderr is console or a file?

I just want to redirect stderr to a file instead of console. I don't want to show many logs in the console, which is annoying to users who don't care and is not good for developers to check the logs in the future.

Thanks in advance

Sonic Ning
  • 61
  • 8
  • 1
    Are you sure it's the redirect that's causing this? Notice that the error did make its way into the file, which doesn't make much sense if it were a transport error relating to writing to the file. – Lightness Races in Orbit Jan 10 '20 at 12:06
  • Thanks, I found out the reason, because I redirected stdout too! I should only redirect stderr! – Sonic Ning Jan 10 '20 at 12:16
  • 1
    oooh I see it uses stdout for responses lol. that's a bit weird – Lightness Races in Orbit Jan 10 '20 at 12:25
  • @LightnessRacesBY-SA3.0 yes, "Starting LSP over stdin/stdout" , it's a little weird. Because there is usually nothing in stdout related to user. Anyway, it might help to check llvm clangd code if someone really want to understand the reason – Sonic Ning Jan 10 '20 at 12:32

1 Answers1

1

I made a stupid and common mistake, "always redirect stdout along with stderr"

like this

> $SOME_PATH/clangd.log 2>&1

I solved it by only redirect stderr to a file, leaving the stdout alone.

the working script is

clangd --clang-tidy -compile-commands-dir=$SOME_PATH --log=error 2> $SOME_PATH/clangd.log
Sonic Ning
  • 61
  • 8