aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2021-11-24 17:28:19 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2021-11-24 17:46:08 +0000
commit4e640edf7642cbc7bd2cc5d308b95cb14f8e236a (patch)
treef5180878a23c12e9ddbb47008f9d9464af08b0b0 /vespalog
parent81affee11ad6a020b3f6a7381c6e6cc3f7cb5123 (diff)
Use c++11 strings instead of old c-style manual string manipulation.
Diffstat (limited to 'vespalog')
-rw-r--r--vespalog/src/logger/runserver.cpp25
-rw-r--r--vespalog/src/vespa/log/control-file.cpp12
-rw-r--r--vespalog/src/vespa/log/control-file.h2
-rw-r--r--vespalog/src/vespa/log/internal.h15
4 files changed, 25 insertions, 29 deletions
diff --git a/vespalog/src/logger/runserver.cpp b/vespalog/src/logger/runserver.cpp
index c74806a8b5b..68950cef4b2 100644
--- a/vespalog/src/logger/runserver.cpp
+++ b/vespalog/src/logger/runserver.cpp
@@ -5,7 +5,6 @@
#include <fcntl.h>
#include <cerrno>
#include <unistd.h>
-#include <csignal>
#include <sys/select.h>
#include <sys/types.h>
@@ -54,13 +53,13 @@ bool whole_seconds(int cnt, int secs) {
class PidFile
{
private:
- char *_pidfile;
+ std::string _pidfile;
int _fd;
PidFile(const PidFile&);
PidFile& operator= (const PidFile&);
public:
- PidFile(const char *pidfile) : _pidfile(strdup(pidfile)), _fd(-1) {}
- ~PidFile() { free(_pidfile); if (_fd >= 0) close(_fd); }
+ PidFile(const char *pidfile) : _pidfile(pidfile), _fd(-1) {}
+ ~PidFile() { if (_fd >= 0) close(_fd); }
int readPid();
void writePid();
bool writeOpen();
@@ -72,7 +71,7 @@ public:
void
PidFile::cleanUp()
{
- if (!anotherRunning()) remove(_pidfile);
+ if (!anotherRunning()) remove(_pidfile.c_str());
if (_fd >= 0) close(_fd);
_fd = -1;
}
@@ -82,14 +81,14 @@ PidFile::writeOpen()
{
if (_fd >= 0) close(_fd);
int flags = O_CREAT | O_WRONLY | O_NONBLOCK;
- _fd = open(_pidfile, flags, 0644);
+ _fd = open(_pidfile.c_str(), flags, 0644);
if (_fd < 0) {
- fprintf(stderr, "could not create pidfile %s: %s\n", _pidfile,
+ fprintf(stderr, "could not create pidfile %s: %s\n", _pidfile.c_str(),
strerror(errno));
return false;
}
if (flock(_fd, LOCK_EX | LOCK_NB) != 0) {
- fprintf(stderr, "could not lock pidfile %s: %s\n", _pidfile,
+ fprintf(stderr, "could not lock pidfile %s: %s\n", _pidfile.c_str(),
strerror(errno));
close(_fd);
_fd = -1;
@@ -106,7 +105,7 @@ PidFile::writePid()
int didtruncate = ftruncate(_fd, (off_t)0);
if (didtruncate != 0) {
fprintf(stderr, "could not truncate pid file %s: %s\n",
- _pidfile, strerror(errno));
+ _pidfile.c_str(), strerror(errno));
std::_Exit(1);
}
char buf[100];
@@ -115,16 +114,16 @@ PidFile::writePid()
ssize_t didw = write(_fd, buf, l);
if (didw != l) {
fprintf(stderr, "could not write pid to %s: %s\n",
- _pidfile, strerror(errno));
+ _pidfile.c_str(), strerror(errno));
std::_Exit(1);
}
- LOG(debug, "wrote '%s' to %s (fd %d)", buf, _pidfile, _fd);
+ LOG(debug, "wrote '%s' to %s (fd %d)", buf, _pidfile.c_str(), _fd);
}
int
PidFile::readPid()
{
- FILE *pf = fopen(_pidfile, "r");
+ FILE *pf = fopen(_pidfile.c_str(), "r");
if (pf == NULL) return 0;
char buf[100];
strcpy(buf, "0");
@@ -151,7 +150,7 @@ bool
PidFile::canStealLock()
{
int flags = O_WRONLY | O_NONBLOCK;
- int desc = open(_pidfile, flags, 0644);
+ int desc = open(_pidfile.c_str(), flags, 0644);
if (desc < 0) {
return false;
}
diff --git a/vespalog/src/vespa/log/control-file.cpp b/vespalog/src/vespa/log/control-file.cpp
index 77ad1d0ec73..2096dd1531c 100644
--- a/vespalog/src/vespa/log/control-file.cpp
+++ b/vespalog/src/vespa/log/control-file.cpp
@@ -5,7 +5,6 @@
#include <ctype.h>
#include <cstdio>
#include <sys/mman.h>
-#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <memory>
@@ -28,7 +27,7 @@ ControlFile::ControlFile(const char *file, Mode mode)
: (O_RDWR | O_CREAT))),
_fileSize(0),
_mode(mode),
- _fileName(strdup(file)),
+ _fileName(file),
_prefix(0),
_mapBase(0),
_mappedSize(0),
@@ -43,7 +42,6 @@ ControlFile::ControlFile(const char *file, Mode mode)
ControlFile::~ControlFile()
{
freeMapping();
- free(_fileName);
}
void
@@ -168,7 +166,7 @@ ControlFile::extendMapping()
if (fileLen == -1) {
_fileBacking.unlock();
- LOG(error, "Cannot get file size of '%s': %s", _fileName,
+ LOG(error, "Cannot get file size of '%s': %s", _fileName.c_str(),
strerror(errno));
return false;
}
@@ -273,14 +271,14 @@ ControlFile::getLevels(const char *name)
strcat(appendedString, "\n");
int len = strlen(appendedString);
- int fd = open(_fileName, O_WRONLY | O_APPEND);
+ int fd = open(_fileName.c_str(), O_WRONLY | O_APPEND);
int wlen = write(fd, appendedString, len);
oldFileLength = lseek(fd, (off_t)0, SEEK_CUR) - wlen;
close(fd);
if (wlen != len) {
_fileBacking.unlock();
LOG(error, "Writing to control file '%s' fails (%d/%d bytes): %s",
- _fileName, wlen, len, strerror(errno));
+ _fileName.c_str(), wlen, len, strerror(errno));
return reinterpret_cast<unsigned int *>(inheritLevels);
} else {
_fileSize = _fileBacking.size();
@@ -290,7 +288,7 @@ ControlFile::getLevels(const char *name)
if (!extendMapping()) {
_fileBacking.unlock(); // just for sure
LOG(error, "Failed to extend mapping of '%s', losing runtime "
- "configurability of component '%s'", _fileName, name);
+ "configurability of component '%s'", _fileName.c_str(), name);
return defaultLevels();
}
}
diff --git a/vespalog/src/vespa/log/control-file.h b/vespalog/src/vespa/log/control-file.h
index 6f302c7a97c..69e725dc465 100644
--- a/vespalog/src/vespa/log/control-file.h
+++ b/vespalog/src/vespa/log/control-file.h
@@ -19,7 +19,7 @@ private:
Lock _fileBacking;
int _fileSize;
enum Mode _mode;
- char *_fileName;
+ std::string _fileName;
void ensureHeader();
bool hasPrefix() { return (_prefix != NULL &&
_prefix[0] != '\0' &&
diff --git a/vespalog/src/vespa/log/internal.h b/vespalog/src/vespa/log/internal.h
index c9081b72ce9..4411d9fa6e6 100644
--- a/vespalog/src/vespa/log/internal.h
+++ b/vespalog/src/vespa/log/internal.h
@@ -1,7 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
-#include <cstring>
+#include <string>
#include <cstdlib>
#if !__GNUC__ && !defined(__attribute__)
@@ -15,15 +15,14 @@ void throwInvalid(const char *fmt, ...)
class InvalidLogException {
private:
- char *_what;
- InvalidLogException& operator = (const InvalidLogException&);
+ std::string _what;
public:
- InvalidLogException(const InvalidLogException &x) :
- _what(strdup(x._what)) {}
- InvalidLogException(const char *s) : _what(strdup(s)) {}
- ~InvalidLogException() { free(_what); }
- const char *what() const { return _what; }
+ InvalidLogException& operator = (const InvalidLogException&) = delete;
+ InvalidLogException(const InvalidLogException &x) = default;
+ InvalidLogException(const char *s) : _what(s) {}
+ ~InvalidLogException() = default;
+ const char *what() const { return _what.c_str(); }
};
} // end namespace ns_log