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

Roslibjs logging 211 #299

Merged
merged 11 commits into from
Feb 15, 2020
109 changes: 109 additions & 0 deletions robot/basestation/static/js/roslibjs/ros-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ REQUEST_TIMEOUT = 3000
ROTATE_TIMEOUT = 1000
lastRotate = 0

// ROS logging severity level constants
const ROSDEBUG = 1 // debug level
const ROSINFO = 2 // general level
const ROSWARN = 4 // warning level
const ROSERROR = 8 // error level
const ROSFATAL = 16 // fatal/critical level

function initRosWeb () {
ros = new ROSLIB.Ros({
url: 'ws://' + env.HOST_IP + ':9090'
Expand All @@ -20,6 +27,15 @@ function initRosWeb () {
appendToConsole('Connection to websocket server closed.')
})

/* Logging */

// publishes log messages to /rosout
ros_logger = new ROSLIB.Topic({
ros: ros,
name: 'rosout',
messageType: 'rosgraph_msgs/Log'
})

/* general controls */

// setup a client for the ping service
Expand Down Expand Up @@ -188,6 +204,99 @@ function initRosWeb () {
}

/* functions used in main code */

function getTimestamp () {
MartensCedric marked this conversation as resolved.
Show resolved Hide resolved
// next few lines taken and adjusted from roslibjs action server example
let currentTime = new Date()
let secs = currentTime.getTime()/1000 // seconds before truncating the decimal
let secsFloored = Math.floor(secs) // seconds after truncating
let nsecs = Math.round(1000000000*(secs-secsFloored)) // nanoseconds since the previous second
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I say rename this nanoSecs to make the name clearer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


// return a dictionary for the ROS log and a float for the gui console
return [{secs : secsFloored, nsecs : nsecs}, currentTime]
}

function logGeneric (logLevel, message) {
MartensCedric marked this conversation as resolved.
Show resolved Hide resolved
stamps = getTimestamp()
rosTimestamp = stamps[0] // dictionary for the ROS log message
consoleTimestamp = stamps[1].toString().split(' ')[4] // hh:mm:ss from date object

// @TODO cedric can help me refactor this
switch (logLevel) {
case ROSDEBUG:
consoleMsg = "[DEBUG] " + '[' + consoleTimestamp + ']' + ': ' + message
console.log(consoleMsg)
appendToConsole(consoleMsg, false)
break
case ROSINFO:
consoleMsg = "[INFO] " + '[' + consoleTimestamp + ']' + ': ' + message
console.log(consoleMsg)
appendToConsole(consoleMsg, false)
break
case ROSWARN:
consoleMsg = "[WARN] " + '[' + consoleTimestamp + ']' + ': ' + message
console.error(consoleMsg)
appendToConsole(consoleMsg, false)
break
case ROSERROR:
consoleMsg = "[ERROR] " + '[' + consoleTimestamp + ']' + ': ' + message
console.error(consoleMsg)
appendToConsole(consoleMsg, false)
break
case ROSFATAL:
consoleMsg = "[FATAL] " + '[' + consoleTimestamp + ']' + ': ' + message
console.error(consoleMsg)
appendToConsole(consoleMsg, false)
break
}

// @TODO bonus if we can determine how to generate a log file for the gui
// rather than simply having it show up in rosout.log
// an attempt was made with the name and file elements in the log message
ros_logger.publish(
new ROSLIB.Message({
// I'm only publishing the essentials. We could include more info if so desired
header : {
// seq // uint32: sequence ID, seems to increment automatically
stamp : rosTimestamp
// frame_id // string: probably only useful for tf
},
level : logLevel,
// name : '/web_gui', // name of the node
msg : message,
// file : 'web_gui' // string: we could specify the js file that generated the log
// function // string: we could specify the parent function that generated the log
// line // uint32: we could specify the specific line of code that generated the log
// topics // string[]: topic names that the node publishes
})
)
}

// Copying how rospy handles the logging functions:
// All log messages should go to the log file (but how?)
// debug messages only go to /rosout if a value is set while initializing the js
// info goes to stdout, warn-error-fatal go to stderr
// rospy also takes in *args and **kwargs, how does this work and do I need to implement that?
function logDebug (message) {
// @TODO only log debug messages if we set a parameter somewhere.
MartensCedric marked this conversation as resolved.
Show resolved Hide resolved
// in rospy this parameter is set when you initialize your node
// with: rospy.init_node(nodeName, log_level=rospy.DEBUG)
// for the webpage maybe we can set it when we run initRosWeb()
logGeneric(ROSDEBUG, message)
}
function logInfo (message) {
logGeneric(ROSINFO, message)
}
function logWarn (message) {
logGeneric(ROSWARN, message)
}
function logErr (message) {
logGeneric(ROSERROR, message)
}
function logFatal (message) {
logGeneric(ROSFATAL, message)
}

function requestMuxChannel (elemID, callback, timeout = REQUEST_TIMEOUT) {
let dev = elemID[elemID.length - 1]
let request = new ROSLIB.ServiceRequest({ device: dev })
Expand Down