|
| 1 | +// Copyright (c) 2026, The Robot Web Tools Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const Logging = require('./logging.js'); |
| 18 | + |
| 19 | +const LOGGING_SEVERITY_UNSET = 0; |
| 20 | + |
| 21 | +/** |
| 22 | + * Implements the ROS 2 logging service interfaces for a node. |
| 23 | + * |
| 24 | + * The interfaces implemented are: |
| 25 | + * rcl_interfaces/srv/GetLoggerLevels |
| 26 | + * rcl_interfaces/srv/SetLoggerLevels |
| 27 | + * |
| 28 | + * @class |
| 29 | + */ |
| 30 | +class LoggingService { |
| 31 | + /** |
| 32 | + * Create a new instance. |
| 33 | + * @param {Node} node - The node these services support. |
| 34 | + */ |
| 35 | + constructor(node) { |
| 36 | + this._node = node; |
| 37 | + this._isRunning = false; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Get the node this service supports. |
| 42 | + * @return {Node} - The supported node. |
| 43 | + */ |
| 44 | + get node() { |
| 45 | + return this._node; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Check if logging services are configured and accepting requests. |
| 50 | + * @return {boolean} - True if services are active; false otherwise. |
| 51 | + */ |
| 52 | + isStarted() { |
| 53 | + return this._isRunning; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Configure logging services and begin processing client requests. |
| 58 | + * @return {undefined} |
| 59 | + */ |
| 60 | + start() { |
| 61 | + if (this._isRunning) return; |
| 62 | + |
| 63 | + this._isRunning = true; |
| 64 | + const nodeName = this.node.name(); |
| 65 | + |
| 66 | + this.node.createService( |
| 67 | + 'rcl_interfaces/srv/GetLoggerLevels', |
| 68 | + nodeName + '/get_logger_levels', |
| 69 | + (request, response) => this._handleGetLoggerLevels(request, response) |
| 70 | + ); |
| 71 | + |
| 72 | + this.node.createService( |
| 73 | + 'rcl_interfaces/srv/SetLoggerLevels', |
| 74 | + nodeName + '/set_logger_levels', |
| 75 | + (request, response) => this._handleSetLoggerLevels(request, response) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + _handleGetLoggerLevels(request, response) { |
| 80 | + const msg = response.template; |
| 81 | + |
| 82 | + for (const name of request.names) { |
| 83 | + try { |
| 84 | + msg.levels.push({ |
| 85 | + name, |
| 86 | + level: Logging.getLogger(name).loggerEffectiveLevel, |
| 87 | + }); |
| 88 | + } catch { |
| 89 | + msg.levels.push({ |
| 90 | + name, |
| 91 | + level: LOGGING_SEVERITY_UNSET, |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + response.send(msg); |
| 97 | + } |
| 98 | + |
| 99 | + _handleSetLoggerLevels(request, response) { |
| 100 | + const msg = response.template; |
| 101 | + |
| 102 | + for (const loggerLevel of request.levels) { |
| 103 | + const result = { |
| 104 | + successful: false, |
| 105 | + reason: '', |
| 106 | + }; |
| 107 | + |
| 108 | + try { |
| 109 | + Logging.getLogger(loggerLevel.name).setLoggerLevel(loggerLevel.level); |
| 110 | + result.successful = true; |
| 111 | + } catch (error) { |
| 112 | + result.reason = error.message; |
| 113 | + } |
| 114 | + |
| 115 | + msg.results.push(result); |
| 116 | + } |
| 117 | + |
| 118 | + response.send(msg); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +module.exports = LoggingService; |
0 commit comments