-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Branch commit log: package.json: add --log2file to synth engine electron/main.js: add --log2file to synth engine ase/utils.hh: include ase/logging.hh ase/main: add --log2file and log info of interest ase/engine.cc: log missing PCM driver as error ase/driver.cc: log driver opening as info ase/logging: add log_setup(), loginf() and logerr() ase/logging: add source files for logging Signed-off-by: Tim Janik <[email protected]>
- Loading branch information
Showing
9 changed files
with
188 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0 | ||
#include "logging.hh" | ||
#include "platform.hh" | ||
#include "path.hh" | ||
#include <stdarg.h> | ||
#include <dirent.h> | ||
#include <fcntl.h> | ||
#include <sys/stat.h> | ||
|
||
namespace Ase { | ||
|
||
static uint64 logstart_timestamp = 0; | ||
static constexpr double USEC2SEC = 1.0 / 1000000; | ||
static bool info2stderr = true; | ||
static int log_fd = -1; | ||
|
||
static std::array<int,2> | ||
log_fds (bool iserror) | ||
{ | ||
if (!logstart_timestamp) | ||
logstart_timestamp = timestamp_startup(); | ||
return { info2stderr || iserror ? 2 : -1, log_fd }; | ||
} | ||
|
||
static char* | ||
sntime (char *buffer, size_t bsize) | ||
{ | ||
buffer[0] = 0; | ||
snprintf (buffer, bsize, "[%+11.6f] ", USEC2SEC * (timestamp_realtime() - logstart_timestamp)); | ||
return buffer + strlen (buffer); | ||
} | ||
|
||
static String | ||
ilog_dir (bool mkdirs = false) | ||
{ | ||
const String ilogdir = Path::join (Path::xdg_dir ("CACHE"), "anklang"); | ||
if (mkdirs) | ||
Path::mkdirs (ilogdir); | ||
return ilogdir; | ||
} | ||
|
||
void | ||
log_setup (bool inf2stderr, bool log2file) | ||
{ | ||
if (log_fd >= 0) return; | ||
info2stderr = inf2stderr; | ||
if (log2file) { | ||
const String dir = ilog_dir (true); | ||
const String fname = string_format ("%s/%s-%08x.log", dir, program_alias(), gethostid()); | ||
errno = EBUSY; | ||
const int OFLAGS = O_CREAT | O_EXCL | O_WRONLY | O_NOCTTY | O_NOFOLLOW | O_CLOEXEC; // O_TRUNC | ||
const int OMODE = 0640; | ||
int fd = open (fname.c_str(), OFLAGS, OMODE); | ||
if (fd < 0 && errno == EEXIST) { | ||
const String oldname = fname + ".old"; | ||
if (rename (fname.c_str(), oldname.c_str()) < 0) | ||
perror (string_format ("%s: failed to rename \"%s\"", program_alias(), oldname.c_str()).c_str()); | ||
fd = open (fname.c_str(), OFLAGS, OMODE); | ||
} | ||
if (fd < 0) | ||
perror (string_format ("%s: failed to open log file \"%s\"", program_alias(), fname.c_str()).c_str()); | ||
else { | ||
log_fd = fd; | ||
const auto lfds = log_fds (false); // does on demand setup | ||
if (lfds[1] >= 0) { | ||
constexpr const int MAXBUFFER = 1024; | ||
char buffer[MAXBUFFER] = { 0, }, *b = sntime (buffer, MAXBUFFER); | ||
snprintf (b, MAXBUFFER - (b - buffer), | ||
"%s %s: pid=%u startup=%.6f\n", | ||
program_alias().c_str(), ase_build_id(), | ||
getpid(), USEC2SEC * logstart_timestamp); | ||
write (lfds[1], buffer, strlen (buffer)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void | ||
logmsg (const char c, const String &dept, const String &msg) | ||
{ | ||
Lib::ScopedPosixLocale posix_locale_scope; // push POSIX locale for this scope | ||
if (msg.empty()) return; | ||
String s = msg; | ||
if (s[s.size()-1] != '\n') | ||
s += "\n"; | ||
if (c == 'E') | ||
s = dept + (dept.empty() ? "" : " ") + "Error: " + s; | ||
else if (!dept.empty()) | ||
s = dept + ": " + s; | ||
for (auto fd : log_fds (c == 'E')) { | ||
if (fd == 1) fflush (stdout); | ||
if (fd == 2) fflush (stderr); | ||
write (fd, s.data(), s.size()); | ||
// fdatasync (fd); | ||
} | ||
} | ||
|
||
} // Ase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0 | ||
#pragma once | ||
|
||
#include <ase/strings.hh> | ||
|
||
namespace Ase { | ||
|
||
/// Write a string_format() message to the log file (or possibly stderr), using the POSIX/C locale. | ||
template<class... A> void loginf (const char *format, const A &...args) ASE_PRINTF (1, 0); | ||
|
||
/// Format and send a log message to the user, stderr and log file, using the POSIX/C locale. | ||
template<class... A> void logerr (const String &dept, const char *format, const A &...args) ASE_PRINTF (2, 0); | ||
|
||
/// Open log file. | ||
void log_setup (bool inf2stderr, bool log2file); | ||
|
||
// == Impl == | ||
void logmsg (const char c, const String &dept, const String &msg); | ||
template<class... A> void | ||
loginf (const char *format, const A &...args) | ||
{ | ||
logmsg ('I', "", string_format (format, args...).c_str()); | ||
} | ||
template<class... A> void | ||
logerr (const String &dept, const char *format, const A &...args) | ||
{ | ||
logmsg ('E', dept, string_format (format, args...).c_str()); | ||
} | ||
|
||
} // Ase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters