Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High level logger FA definitions #1440

Merged
merged 15 commits into from
Aug 20, 2020
68 changes: 53 additions & 15 deletions apps/logsupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cctype>
#include "logsupport.hpp"
#include "../srtcore/srt.h"
#include "../srtcore/utilities.h"

using namespace std;

Expand Down Expand Up @@ -74,6 +75,47 @@ srt_logging::LogLevel::type SrtParseLogLevel(string level)
return LogLevel::type(i->second);
}

struct LogFANames
{
map<string, int> namemap;

struct ToLowerFormat
{
char operator()(char in)
{
if (islower(in))
return in;
if (isupper(in))
return tolower(in);
if (in == '_')
return '-';

throw std::invalid_argument("Wrong FA name - please check logger_defs.inc.cpp file");
}
};

void Install(string upname, int value)
{
string id;
transform(upname.begin(), upname.end(), back_inserter(id), ToLowerFormat());
namemap[id] = value;
}

LogFANames()
{
#define LOGGER(upname, loname, numeric) Install(#upname, SRT_LOGFA_##upname)
#define LOGGER_H LOGGER
#include "../srtcore/logging_defs.inc.cpp"
#undef LOGGER
#undef LOGGER_H
}
} srt_transmit_logfa_names;

const map<string, int> SrtLogFAList()
{
return srt_transmit_logfa_names.namemap;
}

set<srt_logging::LogFA> SrtParseLogFA(string fa, set<string>* punknown)
{
using namespace srt_logging;
Expand All @@ -84,18 +126,17 @@ set<srt_logging::LogFA> SrtParseLogFA(string fa, set<string>* punknown)
if ( fa == "" )
return fas;

static string names [] = { "general", "bstats", "control", "data", "tsbpd", "rexmit", "haicrypt", "cc" };
size_t names_s = sizeof (names)/sizeof (names[0]);
auto& names = srt_transmit_logfa_names.namemap;

if ( fa == "all" )
{
// Skip "general", it's always on
fas.insert(SRT_LOGFA_BSTATS);
fas.insert(SRT_LOGFA_CONTROL);
fas.insert(SRT_LOGFA_DATA);
fas.insert(SRT_LOGFA_TSBPD);
fas.insert(SRT_LOGFA_REXMIT);
fas.insert(SRT_LOGFA_CONGEST);
for (auto entry: names)
{
// Skip "general", it's always on
if (entry.first == "general")
continue;
fas.insert(entry.second);
}
return fas;
}

Expand Down Expand Up @@ -124,8 +165,8 @@ set<srt_logging::LogFA> SrtParseLogFA(string fa, set<string>* punknown)
for (size_t i = 0; i < xfas.size(); ++i)
{
fa = xfas[i];
string* names_p = find(names, names + names_s, fa);
if ( names_p == names + names_s )
int* pfa = map_getp(names, fa);
if (!pfa)
{
if (punknown)
punknown->insert(fa); // If requested, add it back silently
Expand All @@ -134,10 +175,7 @@ set<srt_logging::LogFA> SrtParseLogFA(string fa, set<string>* punknown)
continue;
}

size_t nfa = names_p - names;

if ( nfa != 0 )
fas.insert(nfa);
fas.insert(*pfa);
}

return fas;
Expand Down
1 change: 1 addition & 0 deletions apps/logsupport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

srt_logging::LogLevel::type SrtParseLogLevel(std::string level);
std::set<srt_logging::LogFA> SrtParseLogFA(std::string fa, std::set<std::string>* punknown = nullptr);
const std::map<std::string, int> SrtLogFAList();

SRT_API extern std::map<std::string, int> srt_level_names;

Expand Down
53 changes: 53 additions & 0 deletions scripts/generate-logging-defs.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/tclsh

lassign $argv infile outfile

if {$infile == ""} {
set here [file dirname [info script]]
set infile [file join $here .. srtcore logging_defs.inc.cpp]
}

set id [open $infile]

if {$outfile == ""} {
set od stdout
} else {
set od [open $outfile]
}

set prevval 0

while 1 {
set n [gets $id line]
if {$n == -1} {
break
}

# Cut off comments
set nc [string first // $line]
if {$nc != -1} {
set line [string range $line 0 $nc-1]
}
set line [string trim $line]

# Skip empty lines
if {$line == ""} {
continue
}

set line [string map { ( " \{" ) "\} " } $line]

set arglist [lindex $line 1]
set arglist [regsub -all ",\s*" $arglist " "]

lassign $arglist logsym logmark logval

set form [format "%-20s %d" SRT_LOGFA_$logsym $logval]

if {$logval - $prevval != 1} {
puts $od ""
}

puts $od "#define $form // ${logmark}log"
set prevval $logval
}
31 changes: 17 additions & 14 deletions srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ struct AllFaOn

AllFaOn()
{
// allfa.set(SRT_LOGFA_BSTATS, true);
allfa.set(SRT_LOGFA_CONTROL, true);
allfa.set(SRT_LOGFA_DATA, true);
allfa.set(SRT_LOGFA_TSBPD, true);
allfa.set(SRT_LOGFA_REXMIT, true);
allfa.set(SRT_LOGFA_CONGEST, true);
#define LOGGER(upname, loname, logval) allfa.set(SRT_LOGFA_##upname, true)

// HAICRYPT log is among the hidden ones, this will be defined explicitly.
#define LOGGER_H(upname, loname, logval)
#include "logging_defs.inc.cpp"

#undef LOGGER
#undef LOGGER_H
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

#if ENABLE_HAICRYPT_LOGGING
allfa.set(SRT_LOGFA_HAICRYPT, true);
#endif
Expand All @@ -104,16 +107,16 @@ SRT_API srt_logging::LogConfig srt_logger_config(srt_logging::logger_fa_all.allf

namespace srt_logging
{
#define LOGGER(UPNAME, loname, numeric) \
Logger loname##log(SRT_LOGFA_##UPNAME, srt_logger_config, "SRT." #loname)

// Ignore hidden
#define LOGGER_H(UPNAME, loname, numeric)

Logger glog(SRT_LOGFA_GENERAL, srt_logger_config, "SRT.g");
// Unused. If not found useful, maybe reuse for another FA.
// Logger blog(SRT_LOGFA_BSTATS, srt_logger_config, "SRT.b");
Logger mglog(SRT_LOGFA_CONTROL, srt_logger_config, "SRT.c");
Logger dlog(SRT_LOGFA_DATA, srt_logger_config, "SRT.d");
Logger tslog(SRT_LOGFA_TSBPD, srt_logger_config, "SRT.t");
Logger rxlog(SRT_LOGFA_REXMIT, srt_logger_config, "SRT.r");
Logger cclog(SRT_LOGFA_CONGEST, srt_logger_config, "SRT.cc");
#include "logging_defs.inc.cpp"

#undef LOGGER
#undef LOGGER_H
} // namespace srt_logging

using namespace srt_logging;
Expand Down
18 changes: 18 additions & 0 deletions srtcore/logging_defs.inc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

// This file keeps C++ format, but it should be only included inside a C++ file.
// Prior to #include, the following "LOGGER" macro should be defined:
// #define LOGGER(upname, shortname, numeric) <whatever you need>
// #defing LOGGER_H(upname, shortname, numeric) <same, but for hidden logs>


LOGGER(GENERAL, g, 0);
LOGGER(CONTROL, mg, 2);
LOGGER(DATA, d, 3);
LOGGER(TSBPD, ts, 4);
LOGGER(REXMIT, rx, 5);
// Haicrypt logging - usually off.
LOGGER_H(HAICRYPT,hc, 6);
LOGGER(CONGEST, cc, 7);
// APPLOG=10 - defined in apps, this is only a stub to lock the value
//LOGGER_H(APPLOG, ap, 10);

20 changes: 12 additions & 8 deletions srtcore/srt.h
Original file line number Diff line number Diff line change
Expand Up @@ -577,14 +577,18 @@ enum SRT_REJECT_REASON
// Note that 0 is "general".

// Made by #define so that it's available also for C API.
#define SRT_LOGFA_GENERAL 0
#define SRT_LOGFA_BSTATS 1
#define SRT_LOGFA_CONTROL 2
#define SRT_LOGFA_DATA 3
#define SRT_LOGFA_TSBPD 4
#define SRT_LOGFA_REXMIT 5
#define SRT_LOGFA_HAICRYPT 6
#define SRT_LOGFA_CONGEST 7
// NOTE:
// Use ../scripts/generate-logging-defs.tcl to regenerate.

#define SRT_LOGFA_GENERAL 0 // glog

#define SRT_LOGFA_CONTROL 2 // mglog
#define SRT_LOGFA_DATA 3 // dlog
#define SRT_LOGFA_TSBPD 4 // tslog
#define SRT_LOGFA_REXMIT 5 // rxlog
#define SRT_LOGFA_HAICRYPT 6 // hclog
#define SRT_LOGFA_CONGEST 7 // cclog


// To make a typical int32_t size, although still use std::bitset.
// C API will carry it over.
Expand Down
18 changes: 18 additions & 0 deletions testing/srt-test-live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,24 @@ int main( int argc, char** argv )
cerr << "Example: `-lfa ~all cc` - turns off all FA, except cc\n";
cerr << "Areas: general bstats control data tsbpd rexmit haicrypt cc\n";
cerr << "Default: all are on except haicrypt. NOTE: 'general' can't be off.\n\n";
cerr << "List of functional areas:\n";

map<int, string> revmap;
for (auto entry: SrtLogFAList())
revmap[entry.second] = entry.first;

int en10 = 0;
for (auto entry: revmap)
{
cerr << " " << entry.second;
if (entry.first/10 != en10)
{
cerr << endl;
en10 = entry.first/10;
}
}
cerr << endl;

return 1;
}

Expand Down