1 #include "../include/filebuf.hpp"
2 #include "../include/util.hpp"
3 #if !defined CSDBG_WITH_PLUGIN && !defined CSDBG_WITH_HIGHLIGHT
4 #include "../include/exception.hpp"
29 throw exception(
"invalid argument: path (=%p)", path);
121 if (len > strlen(
m_path)) {
145 return open(O_WRONLY | O_CREAT | O_APPEND, 0644);
174 "failed to open file '%s' (errno %d - %s)",
186 "failed to stat path '%s' (errno %d - %s)",
225 "failed to write data to file '%s' (errno %d - %s)",
267 return const_cast<filebuf&
> (*this);
284 i32 whence = (rel) ? SEEK_CUR : SEEK_SET;
288 "failed to seek file '%s' to offset %d (errno %d - %s)",
319 while (
unlikely(retval < 0 && errno == EINTR) );
358 if (
unlikely(fmt == NULL || strlen(fmt) == 0) )
362 gettimeofday(&now, NULL);
363 u64 tstamp =
static_cast<u64> (now.tv_sec) * 10e+5 + now.tv_usec;
366 string *retval = NULL;
372 for (
u32 i = 0, len = strlen(fmt);
likely(i < len); i++) {
374 if (
likely(ch !=
'%') ) {
380 throw exception(
"invalid format '%s' (at %d: no specifier)", fmt, ++i);
397 retval->
append(
"%x", getpid());
401 retval->
append(
"%lx", pthread_self());
405 retval->
append(
"%lx", tstamp);
409 const i8 err[] =
"invalid format '%s' (at %d: unknown specifier '%c')";
virtual string & append(const string &)
Append a string.
virtual ~filebuf()
Object destructor.
virtual streambuf & flush()=0
To be implemented.
static const i8 * exec_path()
Get the absolute path of the executable.
static bool is_writable(const fileinfo_t &)
Check if the process has write access to a file.
This abstract class is the base for all buffered output stream types (for files, sockets, serial interfaces e.t.c)
virtual filebuf & flush()
Flush the buffered data to the file.
char i8
8-bit signed integer
virtual filebuf & seek_to(i32, bool=false)
Seek the file pointer to a new position.
struct stat fileinfo_t
File metadata.
virtual filebuf & operator=(const filebuf &)
Assignment operator.
unsigned long long u64
64-bit unsigned integer
i32 m_handle
Stream handle (descriptor)
virtual filebuf * clone() const
Object virtual copy constructor.
virtual filebuf & open()
Open the file for output.
string(u32=0)
Object constructor.
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
virtual streambuf & operator=(const streambuf &)
Assignment operator.
virtual filebuf & resize(u32)
Resize the file.
virtual filebuf & sync() const
Commit cached data to the file.
virtual const i8 * path() const
Get the output file path.
unsigned int u32
32-bit unsigned integer
int i32
32-bit signed integer
static bool is_regular(const fileinfo_t &)
Check if a file is a regular one.
A buffered file output stream.
i8 * m_path
Output file path.
filebuf(const i8 *)
Object constructor.
This class is a throwable with a textual description of an error.
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
virtual streambuf & close()
Close the stream.
static string * unique_id(const i8 *)
Create a unique ID based on process identifiers arranged as indicated by a printf-style format string...
#define __D_ASSERT(x)
Assertion macro.