1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-22 02:09:36 +01:00

makeJSONLogger(): Support logging to a Unix domain socket

This commit is contained in:
Eelco Dolstra 2025-03-13 15:48:52 +01:00
parent d9730fc93b
commit 220000dc1a
4 changed files with 18 additions and 4 deletions

View file

@ -7,6 +7,7 @@
#include "source-path.hh"
#include "position.hh"
#include "sync.hh"
#include "unix-domain-socket.hh"
#include <atomic>
#include <sstream>
@ -296,7 +297,10 @@ Logger * makeJSONLogger(const std::filesystem::path & path, bool includeNixPrefi
{ }
};
AutoCloseFD fd{toDescriptor(open(path.c_str(), O_CREAT | O_APPEND | O_WRONLY, 0644))};
AutoCloseFD fd =
std::filesystem::is_socket(path)
? connect(path)
: toDescriptor(open(path.c_str(), O_CREAT | O_APPEND | O_WRONLY, 0644));
if (!fd)
throw SysError("opening log file '%1%'", path);

View file

@ -114,4 +114,11 @@ void connect(Socket fd, const std::string & path)
bindConnectProcHelper("connect", ::connect, fd, path);
}
AutoCloseFD connect(const std::filesystem::path & path)
{
auto fd = createUnixDomainSocket();
nix::connect(toSocket(fd.get()), path);
return fd;
}
}

View file

@ -80,4 +80,9 @@ void bind(Socket fd, const std::string & path);
*/
void connect(Socket fd, const std::string & path);
/**
* Connect to a Unix domain socket.
*/
AutoCloseFD connect(const std::filesystem::path & path);
}