From 438c1add9de7aff5968add3b9e02fac30a62e74a Mon Sep 17 00:00:00 2001 From: "J. Alexander Treuman" Date: Mon, 8 Jan 2007 04:31:44 +0000 Subject: Convert log messages sent to stdout to the current locale's charset. git-svn-id: https://svn.musicpd.org/mpd/trunk@5227 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/localization.c | 16 +++++++++++++++- src/localization.h | 2 ++ src/log.c | 13 +++++++++++-- src/main.c | 2 +- src/path.c | 10 +++------- 5 files changed, 32 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/localization.c b/src/localization.c index 23250a6a9..f2f51d911 100644 --- a/src/localization.c +++ b/src/localization.c @@ -17,6 +17,7 @@ */ #include "localization.h" +#include "charConv.h" #include "utils.h" #include @@ -30,6 +31,19 @@ static char *localeCharset = NULL; +char *utf8ToLocaleCharset(char *str) +{ + static char *ret = NULL; + + if (localeCharset) + ret = convCharset(localeCharset, "UTF-8", str, ret); + + if (!ret) + ret = xstrdup(str); + + return ret; +} + void setLocaleCharset(char *charset) { if (localeCharset) @@ -63,7 +77,7 @@ void initLocalization(void) strcmp(currentLocale, "POSIX") == 0) { WARNING("current locale is \"%s\"\n", currentLocale); - setLocaleCharset(xstrdup("")); + setLocaleCharset(xstrdup("ISO-8859-1")); } else if ((temp = nl_langinfo(CODESET))) { setLocaleCharset(xstrdup(temp)); } else { diff --git a/src/localization.h b/src/localization.h index b70692358..75a0b0b68 100644 --- a/src/localization.h +++ b/src/localization.h @@ -16,6 +16,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +char *utf8ToLocaleCharset(char *str); + void setLocaleCharset(char *charset); char *getLocaleCharset(void); diff --git a/src/log.c b/src/log.c index 2e6eb3575..d38de4f25 100644 --- a/src/log.c +++ b/src/log.c @@ -21,6 +21,7 @@ #include "conf.h" #include "myfprintf.h" #include "utils.h" +#include "localization.h" #include #include @@ -86,9 +87,17 @@ static void buffer_warning(const char *fmt, va_list args) static void do_log(FILE *fp, const char *fmt, va_list args) { - if (!stdout_mode) + char buffer[BUFFER_LENGTH + 1]; + char *localized; + + if (!stdout_mode) { fwrite(log_date(), 15, 1, fp); - vfprintf(fp, fmt, args); + vfprintf(fp, fmt, args); + } else { + vsnprintf(buffer, BUFFER_LENGTH, fmt, args); + localized = utf8ToLocaleCharset(buffer); + fputs(localized, fp); + } } void flushWarningLog(void) diff --git a/src/main.c b/src/main.c index a09e8a6ad..e32146e6c 100644 --- a/src/main.c +++ b/src/main.c @@ -429,6 +429,7 @@ int main(int argc, char *argv[]) initStats(); initTagConfig(); + initLocalization(); initLog(options.verbose); if (options.createDB <= 0 && !options.updateDB) @@ -438,7 +439,6 @@ int main(int argc, char *argv[]) open_log_files(options.stdOutput); - initLocalization(); initPaths(); initPermissions(); initPlaylist(); diff --git a/src/path.c b/src/path.c index 35836ea46..21597f5e6 100644 --- a/src/path.c +++ b/src/path.c @@ -143,14 +143,10 @@ void initPaths(void) } closedir(dir); - if (fsCharsetParam) { + if (fsCharsetParam) charset = xstrdup(fsCharsetParam->value); - } else if ((charset = getLocaleCharset())) { - if (*charset == '\0') - charset = NULL; - else - charset = xstrdup(charset); - } + else if ((charset = getLocaleCharset())) + charset = xstrdup(charset); if (charset) { setFsCharset(charset); -- cgit v1.2.3