mirror of https://github.com/pvnis/srsRAN_4G.git
Import of srslog into srsepc and srsenb. (#1574)
* - Import of srslog into srsepc and srsenb. * - Removed logger_file and logger_stdout files.master
parent
21e9a3958f
commit
cfd3f51931
@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2020 Software Radio Systems Limited
|
|
||||||
*
|
|
||||||
* This file is part of srsLTE.
|
|
||||||
*
|
|
||||||
* srsLTE is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as
|
|
||||||
* published by the Free Software Foundation, either version 3 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* srsLTE is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* A copy of the GNU Affero General Public License can be found in
|
|
||||||
* the LICENSE file in the top-level directory of this distribution
|
|
||||||
* and at http://www.gnu.org/licenses/.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* File: logger_file.h
|
|
||||||
* Description: Common log object. Maintains a queue of log messages
|
|
||||||
* and runs a thread to read messages and write to file.
|
|
||||||
* Multiple producers, single consumer. If full, producers
|
|
||||||
* increase queue size. If empty, consumer blocks.
|
|
||||||
*****************************************************************************/
|
|
||||||
|
|
||||||
#ifndef SRSLTE_LOGGER_FILE_H
|
|
||||||
#define SRSLTE_LOGGER_FILE_H
|
|
||||||
|
|
||||||
#include "srslte/common/logger.h"
|
|
||||||
#include "srslte/common/threads.h"
|
|
||||||
#include <deque>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace srslte {
|
|
||||||
|
|
||||||
typedef std::string* str_ptr;
|
|
||||||
|
|
||||||
class logger_file : public thread, public logger
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
logger_file();
|
|
||||||
logger_file(std::string file);
|
|
||||||
~logger_file();
|
|
||||||
void init(std::string file, int max_length = -1);
|
|
||||||
void stop();
|
|
||||||
// Implementation of log_out
|
|
||||||
void log(unique_log_str_t msg);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void run_thread();
|
|
||||||
void flush();
|
|
||||||
|
|
||||||
uint32_t name_idx;
|
|
||||||
int64_t max_length;
|
|
||||||
int64_t cur_length;
|
|
||||||
FILE* logfile;
|
|
||||||
bool is_running;
|
|
||||||
std::string filename;
|
|
||||||
pthread_cond_t not_empty;
|
|
||||||
pthread_mutex_t mutex;
|
|
||||||
|
|
||||||
std::deque<unique_log_str_t> buffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace srslte
|
|
||||||
|
|
||||||
#endif // SRSLTE_LOGGER_FILE_H
|
|
@ -1,44 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2020 Software Radio Systems Limited
|
|
||||||
*
|
|
||||||
* This file is part of srsLTE.
|
|
||||||
*
|
|
||||||
* srsLTE is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as
|
|
||||||
* published by the Free Software Foundation, either version 3 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* srsLTE is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* A copy of the GNU Affero General Public License can be found in
|
|
||||||
* the LICENSE file in the top-level directory of this distribution
|
|
||||||
* and at http://www.gnu.org/licenses/.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
* File: logger_stdout.h
|
|
||||||
* Description: Interface for logging output
|
|
||||||
*****************************************************************************/
|
|
||||||
|
|
||||||
#ifndef SRSLTE_LOGGER_STDOUT_H
|
|
||||||
#define SRSLTE_LOGGER_STDOUT_H
|
|
||||||
|
|
||||||
#include "srslte/common/logger.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace srslte {
|
|
||||||
|
|
||||||
class logger_stdout : public logger
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void log(unique_log_str_t log_str) { fprintf(stdout, "%s", log_str->str()); }
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace srslte
|
|
||||||
|
|
||||||
#endif // SRSLTE_LOGGER_STDOUT_H
|
|
@ -1,143 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2020 Software Radio Systems Limited
|
|
||||||
*
|
|
||||||
* This file is part of srsLTE.
|
|
||||||
*
|
|
||||||
* srsLTE is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as
|
|
||||||
* published by the Free Software Foundation, either version 3 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* srsLTE is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* A copy of the GNU Affero General Public License can be found in
|
|
||||||
* the LICENSE file in the top-level directory of this distribution
|
|
||||||
* and at http://www.gnu.org/licenses/.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define LOG_BUFFER_SIZE 1024 * 32
|
|
||||||
|
|
||||||
#include "srslte/common/logger_file.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
namespace srslte {
|
|
||||||
|
|
||||||
logger_file::logger_file() : logfile(NULL), is_running(false), cur_length(0), max_length(0), thread("LOGGER_FILE")
|
|
||||||
{
|
|
||||||
pthread_mutex_init(&mutex, NULL);
|
|
||||||
pthread_cond_init(¬_empty, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger_file::~logger_file()
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
pthread_mutex_destroy(&mutex);
|
|
||||||
pthread_cond_destroy(¬_empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
void logger_file::init(std::string file, int max_length_)
|
|
||||||
{
|
|
||||||
if (is_running) {
|
|
||||||
fprintf(stderr, "Error: logger thread is already running.\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
max_length = (int64_t)max_length_ * 1024;
|
|
||||||
name_idx = 0;
|
|
||||||
filename = file;
|
|
||||||
logfile = fopen(filename.c_str(), "w");
|
|
||||||
if (logfile == NULL) {
|
|
||||||
printf("Error: could not create log file, no messages will be logged!\n");
|
|
||||||
}
|
|
||||||
is_running = true;
|
|
||||||
start(-2);
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
void logger_file::stop()
|
|
||||||
{
|
|
||||||
if (is_running) {
|
|
||||||
logger::log_char("Closing log\n");
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
is_running = false;
|
|
||||||
pthread_cond_signal(¬_empty); // wakeup thread and let it terminate
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
wait_thread_finish();
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
flush();
|
|
||||||
if (logfile) {
|
|
||||||
fclose(logfile);
|
|
||||||
logfile = NULL;
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
} else {
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
flush(); // flush even if thread isn't running anymore
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void logger_file::log(unique_log_str_t msg)
|
|
||||||
{
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
buffer.push_back(std::move(msg));
|
|
||||||
pthread_cond_signal(¬_empty);
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
void logger_file::run_thread()
|
|
||||||
{
|
|
||||||
while (is_running) {
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
while (buffer.empty()) {
|
|
||||||
pthread_cond_wait(¬_empty, &mutex);
|
|
||||||
if (!is_running) {
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
return; // Thread done. Messages in buffer will be handled in flush.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unique_log_str_t s = std::move(buffer.front());
|
|
||||||
|
|
||||||
int n = 0;
|
|
||||||
if (logfile) {
|
|
||||||
n = fprintf(logfile, "%s", s->str());
|
|
||||||
}
|
|
||||||
buffer.pop_front();
|
|
||||||
|
|
||||||
if (n > 0) {
|
|
||||||
cur_length += (int64_t)n;
|
|
||||||
if (cur_length >= max_length && max_length > 0) {
|
|
||||||
fclose(logfile);
|
|
||||||
name_idx++;
|
|
||||||
char numstr[21]; // enough to hold all numbers up to 64-bits
|
|
||||||
sprintf(numstr, ".%d", name_idx);
|
|
||||||
string newfilename = filename + numstr;
|
|
||||||
logfile = fopen(newfilename.c_str(), "w");
|
|
||||||
if (logfile == NULL) {
|
|
||||||
printf("Error: could not create log file, no messages will be logged!\n");
|
|
||||||
}
|
|
||||||
cur_length = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void logger_file::flush()
|
|
||||||
{
|
|
||||||
std::deque<unique_log_str_t>::iterator it;
|
|
||||||
for (it = buffer.begin(); it != buffer.end(); it++) {
|
|
||||||
unique_log_str_t s = std::move(*it);
|
|
||||||
if (logfile) {
|
|
||||||
fprintf(logfile, "%s", s->str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buffer.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace srslte
|
|
@ -1,115 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2020 Software Radio Systems Limited
|
|
||||||
*
|
|
||||||
* This file is part of srsLTE.
|
|
||||||
*
|
|
||||||
* srsLTE is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as
|
|
||||||
* published by the Free Software Foundation, either version 3 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* srsLTE is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* A copy of the GNU Affero General Public License can be found in
|
|
||||||
* the LICENSE file in the top-level directory of this distribution
|
|
||||||
* and at http://www.gnu.org/licenses/.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define NTHREADS 100
|
|
||||||
#define NMSGS 100
|
|
||||||
|
|
||||||
#include "srslte/common/logger_file.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
using namespace srslte;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
logger_file* l;
|
|
||||||
int thread_id;
|
|
||||||
} args_t;
|
|
||||||
|
|
||||||
void* thread_loop(void* a)
|
|
||||||
{
|
|
||||||
args_t* args = (args_t*)a;
|
|
||||||
char buf[100];
|
|
||||||
for (int i = 0; i < NMSGS; i++) {
|
|
||||||
sprintf(buf, "Thread %d: %d", args->thread_id, i);
|
|
||||||
args->l->log_char(buf);
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void write(std::string filename)
|
|
||||||
{
|
|
||||||
logger_file l;
|
|
||||||
l.init(filename);
|
|
||||||
pthread_t threads[NTHREADS];
|
|
||||||
args_t args[NTHREADS];
|
|
||||||
for (int i = 0; i < NTHREADS; i++) {
|
|
||||||
args[i].l = &l;
|
|
||||||
args[i].thread_id = i;
|
|
||||||
pthread_create(&threads[i], NULL, &thread_loop, &args[i]);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < NTHREADS; i++) {
|
|
||||||
pthread_join(threads[i], NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool read(std::string filename)
|
|
||||||
{
|
|
||||||
bool pass = true;
|
|
||||||
bool written[NTHREADS][NMSGS];
|
|
||||||
int thread = 0, msg = 0;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
for (int i = 0; i < NTHREADS; i++) {
|
|
||||||
for (int j = 0; j < NMSGS; j++) {
|
|
||||||
written[i][j] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FILE* f = fopen(filename.c_str(), "r");
|
|
||||||
if (f != NULL) {
|
|
||||||
while (fscanf(f, "Thread %d: %d\n", &thread, &msg)) {
|
|
||||||
if (thread < NTHREADS && msg < NMSGS) {
|
|
||||||
written[thread][msg] = true;
|
|
||||||
} else {
|
|
||||||
perror("Wrong thread and/or msg");
|
|
||||||
fclose(f);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fclose(f);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < NTHREADS; i++) {
|
|
||||||
for (int j = 0; j < NMSGS; j++) {
|
|
||||||
if (!written[i][j])
|
|
||||||
pass = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return pass;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
|
||||||
{
|
|
||||||
bool result;
|
|
||||||
std::string f("log.txt");
|
|
||||||
write(f);
|
|
||||||
result = read(f);
|
|
||||||
|
|
||||||
if (remove(f.c_str())) {
|
|
||||||
perror("Removing file");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result) {
|
|
||||||
printf("Passed\n");
|
|
||||||
exit(0);
|
|
||||||
} else {
|
|
||||||
printf("Failed\n;");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue