aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2012-11-15 15:01:02 +0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2012-11-15 15:01:02 +0100
commit8541d27fce95f1f6a4a6c89bcbc09503ff7ea092 (patch)
tree211d86e5d5724703fc5c58e10044b812089d9420 /src/libutil
parent3acc8adcad4066329913cf9ad4e1ccc535f73032 (diff)
Don't use std::cerr in a few places
Slightly scared of using std::cerr in a vforked process...
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc35
-rw-r--r--src/libutil/util.hh4
2 files changed, 23 insertions, 16 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index f311b1ae3..e208701ce 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -483,16 +483,7 @@ void printMsg_(Verbosity level, const format & f)
else if (logType == ltEscapes && level != lvlInfo)
prefix = "\033[" + escVerbosity(level) + "s";
string s = (format("%1%%2%\n") % prefix % f.str()).str();
- try {
- writeToStderr((const unsigned char *) s.data(), s.size());
- } catch (SysError & e) {
- /* Ignore failing writes to stderr if we're in an exception
- handler, otherwise throw an exception. We need to ignore
- write errors in exception handlers to ensure that cleanup
- code runs to completion if the other side of stderr has
- been closed unexpectedly. */
- if (!std::uncaught_exception()) throw;
- }
+ writeToStderr(s);
}
@@ -505,13 +496,28 @@ void warnOnce(bool & haveWarned, const format & f)
}
+void writeToStderr(const string & s)
+{
+ try {
+ _writeToStderr((const unsigned char *) s.data(), s.size());
+ } catch (SysError & e) {
+ /* Ignore failing writes to stderr if we're in an exception
+ handler, otherwise throw an exception. We need to ignore
+ write errors in exception handlers to ensure that cleanup
+ code runs to completion if the other side of stderr has
+ been closed unexpectedly. */
+ if (!std::uncaught_exception()) throw;
+ }
+}
+
+
static void defaultWriteToStderr(const unsigned char * buf, size_t count)
{
writeFull(STDERR_FILENO, buf, count);
}
-void (*writeToStderr) (const unsigned char * buf, size_t count) = defaultWriteToStderr;
+void (*_writeToStderr) (const unsigned char * buf, size_t count) = defaultWriteToStderr;
void readFull(int fd, unsigned char * buf, size_t count)
@@ -845,8 +851,7 @@ void killUser(uid_t uid)
}
} catch (std::exception & e) {
- std::cerr << format("killing processes belonging to uid `%1%': %2%")
- % uid % e.what() << std::endl;
+ writeToStderr((format("killing processes belonging to uid `%1%': %2%\n") % uid % e.what()).str());
_exit(1);
}
_exit(0);
@@ -902,7 +907,7 @@ string runProgram(Path program, bool searchPath, const Strings & args)
throw SysError(format("executing `%1%'") % program);
} catch (std::exception & e) {
- std::cerr << "error: " << e.what() << std::endl;
+ writeToStderr("error: " + string(e.what()) + "\n");
}
_exit(1);
}
@@ -1125,5 +1130,5 @@ void ignoreException()
}
}
-
+
}
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 90413b0ef..87b63f6e9 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -148,7 +148,9 @@ void printMsg_(Verbosity level, const format & f);
void warnOnce(bool & haveWarned, const format & f);
-extern void (*writeToStderr) (const unsigned char * buf, size_t count);
+void writeToStderr(const string & s);
+
+extern void (*_writeToStderr) (const unsigned char * buf, size_t count);
/* Wrappers arount read()/write() that read/write exactly the