Skip to content

Commit

Permalink
Merge pull request opencog#47 from ngeiswei/oc-to-sn-from-sn-fork
Browse files Browse the repository at this point in the history
Merge opencog/cogutil into singnet/cogutil (from singnet/cogutil/master fork)
  • Loading branch information
ngeiswei authored Sep 19, 2019
2 parents 7d95518 + bbec7f1 commit db70d95
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 89 deletions.
5 changes: 5 additions & 0 deletions opencog/util/Logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ void Logger::set_print_to_stdout_flag(bool flag)
if (_log_writer) _log_writer->printToStdout = flag;
}

bool Logger::get_print_to_stdout_flag() const
{
return _log_writer and _log_writer->printToStdout;
}

void Logger::set_print_level_flag(bool flag)
{
printLevel = flag;
Expand Down
5 changes: 5 additions & 0 deletions opencog/util/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ class Logger
*/
void set_print_to_stdout_flag(bool);

/**
* Get the value of the current print to stdout flag.
*/
bool get_print_to_stdout_flag() const;

/**
* If set, the logging level is printed as a part
* of the message,
Expand Down
35 changes: 35 additions & 0 deletions opencog/util/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,41 @@ template<typename Set> std::set<Set> powerset(const Set& s)
return powerset(s, s.size());
}

/**
* Return the n-fold Cartesian product of a given container with
* itself. For instance if
*
* c = {1, 2, 3}, nfold = 2
*
* the result is
*
* { [1, 1], [1, 2], [1, 3],
[2, 1], [2, 2], [2, 3],
[3, 1], [3, 2], [3, 3] }
*/
template<typename C>
std::set<std::vector<typename C::value_type>> cartesian_product(const C& c,
size_t nfold=2)
{
typedef typename C::value_type T;
// Recursive case
if (nfold > 0) {
std::set<std::vector<T>> res;
std::set<std::vector<T>> cp = cartesian_product(c, nfold - 1);
for (const std::vector<T>& t : cp) {
for (const auto& el : c) {
std::vector<T> tel(t);
tel.push_back(el);
res.insert(tel);
}
}
return res;
}

// Base case
return {{}};
}

/**
* Given a sequence of indexes, and a sequence of elements, return a
* sequence of all elements corresponding to the indexes (in the order
Expand Down
15 changes: 0 additions & 15 deletions scripts/README

This file was deleted.

6 changes: 6 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Scripts
=======

Contains scripts related to cogutil, under the `util` folder, in
particular for manipulating log files, such as filtering according to
component name or log level.
73 changes: 0 additions & 73 deletions scripts/util/component-log.sh

This file was deleted.

104 changes: 104 additions & 0 deletions scripts/util/filter-log.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/bin/bash
#
# Given a component and a log file, filter the log so that only the
# messages from the component are output.

set -u

#############
# Constants #
#############

readonly date_re='[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}'
readonly time_re='[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{3}'
readonly timestamp_re="\[${date_re} ${time_re}\]"
readonly anylevel_re="\[(ERROR|WARN|INFO|DEBUG|FINE)\]"

#############
# Functions #
#############

keep_printing=n # for printing multiline messages
grep_component_lines()
{
local line="$1"
if [[ "$line" =~ $inline_re ]]; then
keep_printing=y
elif [[ "$line" =~ $outline_re ]]; then
keep_printing=n
fi
if [[ $keep_printing == y ]]; then
echo "$line"
fi
}

usage()
{
echo "Description: Filter log file."
echo "Usage: $0 [-h|--help|-?] [-l LEVEL] [-c COMPONENT] [LOG_FILE]"
echo " -l Filter by log level. Only messages of level LEVEL are kept."
echo " -c Filter by component name. Only messages from COMPONENT are kept."
echo " -h|--help|-? Show this message."
echo "If both -l and -c are used only messages of that level and component are kept."
echo "If LOG_FILE is not provided then stdin is used."
}

########
# Main #
########

# Parse command arguments

if [[ $# == 0 || $# > 5 ]]; then
echo "Error: Wrong number of arguments"
usage
exit 1
fi

if [[ $1 == "-h" || $1 == "--help" || $1 == "-?" ]]; then
usage
exit 0
fi

LEVEL=""
COMPONENT=""
UNKNOWN_FLAGS=""
while getopts "l:c:" flag ; do
case $flag in
l) LEVEL="${OPTARG^^}" ;;
c) COMPONENT="$OPTARG" ;;
*) UNKNOWN_FLAGS=true ;;
esac
done

shift $((OPTIND-1))

set +u
LOGFILE="$1"
set -u

# Filter

if [[ -n "$LEVEL" ]]; then
readonly level_re="\[$LEVEL\]"
else
readonly level_re="$anylevel_re"
fi
readonly outline_re="$timestamp_re $anylevel_re .*"

if [[ -n "$COMPONENT" ]]; then
readonly component_re="\[$COMPONENT\]"
readonly inline_re="$timestamp_re $level_re $component_re .*"
else
readonly inline_re="$timestamp_re $level_re .*"
fi

if [[ "$LOGFILE" > 0 ]]; then
while read; do
grep_component_lines "$REPLY"
done < "$LOGFILE"
else
while read; do
grep_component_lines "$REPLY"
done
fi
20 changes: 19 additions & 1 deletion tests/util/LoggerUTest.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ public:
// Define a second logger, write some massive message on the first
// one, a small message on the second, and see if the message of
// the second scrumbles the first message or appears after it.
void testLoggerInteraction() {
void testLoggerInteraction()
{
logger().set_level(Logger::DEBUG);
logger().set_timestamp_flag(false);
Logger my_logger;
Expand Down Expand Up @@ -279,4 +280,21 @@ public:
TS_ASSERT(resline == prefix + message);
}

// Define a second logger, enable stdout in one logger, and check
// whether the stdout in the other logger is left unchanged.
//
// TODO: re-enabled once fixed
void xtestLoggerStdoutFlagInteraction()
{
Logger my_logger;

// Set different stdout flags for my_logger and the default logger
logger().set_print_to_stdout_flag(false);
my_logger.set_print_to_stdout_flag(true);

// Make sure their flags differ
TS_ASSERT_DIFFERS(logger().get_print_to_stdout_flag(),
my_logger.get_print_to_stdout_flag());
}

}; // class
9 changes: 9 additions & 0 deletions tests/util/algorithmUTest.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ public:
TS_ASSERT_EQUALS(ps, expect_set);
}

void test_cartesian_product() {
set<int> c = {1, 2, 3};
set<vector<int>> result = cartesian_product(c, 2);
set<vector<int>> expect = { {1, 1}, {1, 2}, {1, 3},
{2, 1}, {2, 2}, {2, 3},
{3, 1}, {3, 2}, {3, 3} };

TS_ASSERT_EQUALS(result, expect);
}
};

0 comments on commit db70d95

Please sign in to comment.