mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-02-15 14:36:45 +00:00
Merge branch 'moy.logrotate' into smgmaster
This commit is contained in:
commit
cd7499970b
@ -647,12 +647,99 @@ SWITCH_DECLARE(switch_status_t) switch_queue_trypush(switch_queue_t *queue, void
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** File types. */
|
||||
typedef enum {
|
||||
SWITCH_NOFILE = 0, /**< no file type determined */
|
||||
SWITCH_REG, /**< a regular file */
|
||||
SWITCH_DIR, /**< a directory */
|
||||
SWITCH_CHR, /**< a character device */
|
||||
SWITCH_BLK, /**< a block device */
|
||||
SWITCH_PIPE, /**< a FIFO / pipe */
|
||||
SWITCH_LNK, /**< a symbolic link */
|
||||
SWITCH_SOCK, /**< a [unix domain] socket */
|
||||
SWITCH_UNKFILE = 127 /**< a file of some other unknown type */
|
||||
} switch_filetype_e;
|
||||
|
||||
|
||||
/** Structure for referencing files. */
|
||||
typedef struct apr_file_t switch_file_t;
|
||||
|
||||
typedef int32_t switch_fileperms_t;
|
||||
typedef int switch_seek_where_t;
|
||||
|
||||
/**
|
||||
* Structure for determining user ownership.
|
||||
*/
|
||||
#ifdef WIN32
|
||||
typedef PSID switch_uid_t;
|
||||
#else
|
||||
typedef uid_t switch_uid_t;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Structure for determining group ownership.
|
||||
*/
|
||||
#ifdef WIN32
|
||||
typedef PSID switch_gid_t;
|
||||
#else
|
||||
typedef gid_t switch_gid_t;
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
typedef uint32_t switch_dev_t;
|
||||
typedef uint64_t switch_ino_t;
|
||||
#else
|
||||
typedef ino_t switch_ino_t;
|
||||
typedef dev_t switch_dev_t;
|
||||
#endif
|
||||
typedef off_t switch_off_t;
|
||||
|
||||
/**
|
||||
* Structure for referencing file information
|
||||
*/
|
||||
//typedef struct apr_stat_t switch_finfo_t;
|
||||
typedef struct {
|
||||
/** Allocates memory and closes lingering handles in the specified pool */
|
||||
switch_memory_pool_t *pool;
|
||||
/** The bitmask describing valid fields of this switch_finfo_t structure
|
||||
* including all available 'wanted' fields and potentially more */
|
||||
int32_t valid;
|
||||
/** The access permissions of the file. Mimics Unix access rights. */
|
||||
switch_fileperms_t protection;
|
||||
/** The type of file. One of APR_REG, APR_DIR, APR_CHR, APR_BLK, APR_PIPE,
|
||||
* APR_LNK or APR_SOCK. If the type is undetermined, the value is APR_NOFILE.
|
||||
* If the type cannot be determined, the value is APR_UNKFILE.
|
||||
*/
|
||||
switch_filetype_e filetype;
|
||||
/** The user id that owns the file */
|
||||
switch_uid_t user;
|
||||
/** The group id that owns the file */
|
||||
switch_gid_t group;
|
||||
/** The inode of the file. */
|
||||
switch_ino_t inode;
|
||||
/** The id of the device the file is on. */
|
||||
switch_dev_t device;
|
||||
/** The number of hard links to the file. */
|
||||
int32_t nlink;
|
||||
/** The size of the file */
|
||||
switch_off_t size;
|
||||
/** The storage size consumed by the file */
|
||||
switch_off_t csize;
|
||||
/** The time the file was last accessed */
|
||||
switch_time_t atime;
|
||||
/** The time the file was last modified */
|
||||
switch_time_t mtime;
|
||||
/** The time the file was created, or the inode was last changed */
|
||||
switch_time_t ctime;
|
||||
/** The pathname of the file (possibly unrooted) */
|
||||
const char *fname;
|
||||
/** The file's name (no path) in filesystem case */
|
||||
const char *name;
|
||||
/** The file's handle, if accessed (can be submitted to apr_duphandle) */
|
||||
switch_file_t *filehand;
|
||||
} switch_finfo_t;
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup apr_file_seek_flags File Seek Flags
|
||||
* @{
|
||||
@ -739,6 +826,37 @@ SWITCH_DECLARE(switch_status_t) switch_queue_trypush(switch_queue_t *queue, void
|
||||
#define SWITCH_FOPEN_LARGEFILE 0x04000 /**< Platform dependent flag to enable large file support */
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @defgroup switch_file_stat flags
|
||||
* @ingroup switch_file_io
|
||||
* @{
|
||||
*/
|
||||
#define SWITCH_FINFO_LINK 0x00000001 /**< Stat the link not the file itself if it is a link */
|
||||
#define SWITCH_FINFO_MTIME 0x00000010 /**< Modification Time */
|
||||
#define SWITCH_FINFO_CTIME 0x00000020 /**< Creation or inode-changed time */
|
||||
#define SWITCH_FINFO_ATIME 0x00000040 /**< Access Time */
|
||||
#define SWITCH_FINFO_SIZE 0x00000100 /**< Size of the file */
|
||||
#define SWITCH_FINFO_CSIZE 0x00000200 /**< Storage size consumed by the file */
|
||||
#define SWITCH_FINFO_DEV 0x00001000 /**< Device */
|
||||
#define SWITCH_FINFO_INODE 0x00002000 /**< Inode */
|
||||
#define SWITCH_FINFO_NLINK 0x00004000 /**< Number of links */
|
||||
#define SWITCH_FINFO_TYPE 0x00008000 /**< Type */
|
||||
#define SWITCH_FINFO_USER 0x00010000 /**< User */
|
||||
#define SWITCH_FINFO_GROUP 0x00020000 /**< Group */
|
||||
#define SWITCH_FINFO_UPROT 0x00100000 /**< User protection bits */
|
||||
#define SWITCH_FINFO_GPROT 0x00200000 /**< Group protection bits */
|
||||
#define SWITCH_FINFO_WPROT 0x00400000 /**< World protection bits */
|
||||
#define SWITCH_FINFO_ICASE 0x01000000 /**< if dev is case insensitive */
|
||||
#define SWITCH_FINFO_NAME 0x02000000 /**< ->name in proper case */
|
||||
|
||||
#define SWITCH_FINFO_MIN 0x00008170 /**< type, mtime, ctime, atime, size */
|
||||
#define SWITCH_FINFO_IDENT 0x00003000 /**< dev and inode */
|
||||
#define SWITCH_FINFO_OWNER 0x00030000 /**< user and group */
|
||||
#define SWITCH_FINFO_PROT 0x00700000 /**< all protections */
|
||||
#define SWITCH_FINFO_NORM 0x0073b170 /**< an atomic unix apr_stat() */
|
||||
#define SWITCH_FINFO_DIRENT 0x02000000 /**< an atomic unix apr_dir_read() */
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* Open the specified file.
|
||||
* @param newf The opened file descriptor.
|
||||
@ -782,6 +900,8 @@ SWITCH_DECLARE(switch_status_t) switch_file_seek(switch_file_t *thefile, switch_
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_file_copy(const char *from_path, const char *to_path, switch_fileperms_t perms, switch_memory_pool_t *pool);
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_file_stat(switch_finfo_t *finfo, const char *fname, int32_t wanted, switch_memory_pool_t *pool);
|
||||
|
||||
/**
|
||||
* Close the specified file.
|
||||
* @param thefile The file descriptor to close.
|
||||
|
@ -53,6 +53,7 @@ struct logfile_profile {
|
||||
char *name;
|
||||
switch_size_t log_size; /* keep the log size in check for rotation */
|
||||
switch_size_t roll_size; /* the size that we want to rotate the file at */
|
||||
uint32_t max_file_count; /* Max number of log files */
|
||||
char *logfile;
|
||||
switch_file_t *log_afd;
|
||||
switch_hash_t *log_hash;
|
||||
@ -114,6 +115,8 @@ static switch_status_t mod_logfile_openlogfile(logfile_profile_t *profile, switc
|
||||
static switch_status_t mod_logfile_rotate(logfile_profile_t *profile)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
unsigned int fcount = 0;
|
||||
const char *fname = NULL;
|
||||
char *filename = NULL;
|
||||
switch_status_t stat = 0;
|
||||
int64_t offset = 0;
|
||||
@ -121,6 +124,15 @@ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile)
|
||||
switch_time_exp_t tm;
|
||||
char date[80] = "";
|
||||
switch_size_t retsize;
|
||||
char *dend = NULL;
|
||||
char *hay = NULL;
|
||||
switch_dir_t *dirhandle = NULL;
|
||||
switch_time_t wtime = 0;
|
||||
char dirname[128];
|
||||
char filebuf[128];
|
||||
char fullfile[512];
|
||||
char filetodelete[128];
|
||||
switch_finfo_t statinfo;
|
||||
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
||||
|
||||
switch_mutex_lock(globals.mutex);
|
||||
@ -157,6 +169,54 @@ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile)
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n");
|
||||
|
||||
if (profile->max_file_count) {
|
||||
switch_copy_string(dirname, profile->logfile, sizeof(dirname));
|
||||
dend = strstr(dirname, SWITCH_PATH_SEPARATOR);
|
||||
if (dend) {
|
||||
dend++;
|
||||
while ((hay = strstr(dend, SWITCH_PATH_SEPARATOR))) {
|
||||
hay += strlen(SWITCH_PATH_SEPARATOR);
|
||||
dend = hay;
|
||||
}
|
||||
*dend = '\0';
|
||||
scanning:
|
||||
status = switch_dir_open(&dirhandle, dirname, pool);
|
||||
if (status != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't open directory: %s\n", dirname);
|
||||
goto end;
|
||||
}
|
||||
|
||||
wtime = switch_time_now();
|
||||
while ((fname = switch_dir_next_file(dirhandle, filebuf, sizeof(filebuf)))) {
|
||||
memset(&statinfo, 0, sizeof(statinfo));
|
||||
snprintf(fullfile, sizeof(fullfile), "%s%s", dirname, fname);
|
||||
if (strstr(fullfile, profile->logfile)) {
|
||||
if (switch_file_stat(&statinfo, fullfile, SWITCH_FINFO_CTIME, pool) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to stat file %s\n", fullfile);
|
||||
continue;
|
||||
}
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "File: %s has stat %llu, size = %llu\n", fullfile, (unsigned long long)statinfo.ctime, (unsigned long long)statinfo.size);
|
||||
if (statinfo.ctime < wtime) {
|
||||
switch_copy_string(filetodelete, fullfile, sizeof(filetodelete));
|
||||
wtime = statinfo.ctime;
|
||||
}
|
||||
fcount++;
|
||||
}
|
||||
}
|
||||
if (fcount > profile->max_file_count) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Deleting old logfile: %s\n", filetodelete);
|
||||
switch_file_remove(filetodelete, pool);
|
||||
fcount--;
|
||||
}
|
||||
switch_dir_close(dirhandle);
|
||||
if (fcount > profile->max_file_count) {
|
||||
goto scanning;
|
||||
}
|
||||
} else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "No separator %s found in %s\n", SWITCH_PATH_SEPARATOR, dirname);
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
|
||||
if (pool) {
|
||||
@ -271,6 +331,11 @@ static switch_status_t load_profile(switch_xml_t xml)
|
||||
if (new_profile->roll_size < 0) {
|
||||
new_profile->roll_size = 0;
|
||||
}
|
||||
} else if (!strcmp(var, "maxfilecount")) {
|
||||
new_profile->max_file_count = atoi(val);
|
||||
if (new_profile->max_file_count < 1) {
|
||||
new_profile->max_file_count = 1;
|
||||
}
|
||||
} else if (!strcmp(var, "uuid") && switch_true(val)) {
|
||||
new_profile->log_uuid = SWITCH_TRUE;
|
||||
}
|
||||
|
@ -416,6 +416,18 @@ SWITCH_DECLARE(switch_status_t) switch_file_copy(const char *from_path, const ch
|
||||
return apr_file_copy(from_path, to_path, perms, pool);
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_file_stat(switch_finfo_t *finfo, const char *fname, int32_t wanted, switch_memory_pool_t *pool)
|
||||
{
|
||||
apr_status_t status;
|
||||
apr_finfo_t aprinfo;
|
||||
if (sizeof(*finfo) != sizeof(aprinfo)) {
|
||||
return SWITCH_STATUS_MEMERR;
|
||||
}
|
||||
status = apr_stat(&aprinfo, fname, wanted, pool);
|
||||
memcpy(finfo, &aprinfo, sizeof(*finfo));
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_file_close(switch_file_t *thefile)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user