]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add %log.destination()
authorAlan T. DeKok <aland@freeradius.org>
Tue, 21 Nov 2023 23:44:48 +0000 (18:44 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 21 Nov 2023 23:44:48 +0000 (18:44 -0500)
doc/antora/modules/reference/pages/xlat/log.adoc
src/lib/unlang/xlat_builtin.c

index 0fecc57f23f7c21582ffd27e963811cd7e5eb843..0f22431560e448bf920e007bd53ade8991c305e1 100644 (file)
@@ -17,6 +17,30 @@ Logs a message at a DEBUG level.  This function returns nothing.
 
 The DEBUG messages are printed only when the server has the debug flag set.
 
+== %log.destination(_string_, [_uint32_])
+
+The `%log.destination()` function updates the current request to add a new debug log destination.  The second argument sets the debug level (0..4).  If omitted, the default is `2`.  The special value `0` will remove the named log destination.
+
+If the log destination is the empty string (`''`), then all extra debug log destinations are removed, and only the server default log destination will remain.
+
+This function is normally used to _add_ a new log destination.  Any extra debug logging does not change the default log destination.  Normal server log messages will still go to the normal log destination, in addition to being sent to the newly added destination.
+
+An existing log destination can be set multiple times.  The first time it is set, the log destination is added.  The second and subsequent times it is set, the log level is changed.  The server will not send duplicate messages to the same logging destination.
+
+.Log Configuration in radiusd.conf
+----
+log tmp {
+       destination = files
+       filename = ...
+}
+----
+
+.Set log destination in virtual server
+[source,unlang]
+----
+%log.destination('foo')
+----
+
 == %log.err(_string_)
 
 Logs a message at a ERROR level.  This function returns nothing.
index 978be50d831c98bbcaeeea78933d55fab6cea2e2..3d3c57d69c7a65fdda3481561b5650f27c44370d 100644 (file)
@@ -1128,7 +1128,7 @@ static xlat_action_t xlat_func_log_err(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor
 }
 
 
-/** Log something at WARNlevel.
+/** Log something at WARN level.
  *
  * Example:
 @verbatim
@@ -1152,6 +1152,45 @@ static xlat_action_t xlat_func_log_warn(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcurso
        return XLAT_ACTION_DONE;
 }
 
+static xlat_arg_parser_t const xlat_func_log_dst_args[] = {
+       { .required = true, .type = FR_TYPE_STRING },
+       { .required = false, .type = FR_TYPE_UINT32 },
+       XLAT_ARG_PARSER_TERMINATOR
+};
+
+/** Change the log destination to the named one
+ *
+ * Example:
+@verbatim
+%log.destination('foo')
+@endverbatim
+ *
+ * @ingroup xlat_functions
+ */
+static xlat_action_t xlat_func_log_dst(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out,
+                                      UNUSED xlat_ctx_t const *xctx,
+                                      request_t *request, fr_value_box_list_t *args)
+{
+       fr_value_box_t  *dst, *lvl;
+       fr_log_t *log;
+       uint32_t level = 2;
+
+       XLAT_ARGS(args, &dst, &lvl);
+
+       if (!dst || !*dst->vb_strvalue) {
+               request_log_prepend(request, NULL, L_DBG_LVL_OFF);
+               return XLAT_ACTION_DONE;
+       }
+
+       log = log_dst_by_name(dst->vb_strvalue);
+       if (!log) return XLAT_ACTION_FAIL;
+
+       if (lvl) level = lvl->vb_uint32;
+
+       request_log_prepend(request, log, level);
+       return XLAT_ACTION_DONE;
+}
+
 
 static xlat_arg_parser_t const xlat_func_map_arg[] = {
        { .required = true, .concat = true, .type = FR_TYPE_STRING },
@@ -3700,6 +3739,7 @@ do { \
        XLAT_REGISTER_ARGS("log.err", xlat_func_log_err, FR_TYPE_NULL, xlat_func_log_arg);
        XLAT_REGISTER_ARGS("log.info", xlat_func_log_info, FR_TYPE_NULL, xlat_func_log_arg);
        XLAT_REGISTER_ARGS("log.warn", xlat_func_log_warn, FR_TYPE_NULL, xlat_func_log_arg);
+       XLAT_REGISTER_ARGS("log.destination", xlat_func_log_dst, FR_TYPE_STRING, xlat_func_log_dst_args);
        XLAT_REGISTER_ARGS("nexttime", xlat_func_next_time, FR_TYPE_UINT64, xlat_func_next_time_args);
        XLAT_REGISTER_ARGS("pairs", xlat_func_pairs, FR_TYPE_STRING, xlat_func_pairs_args);
        XLAT_REGISTER_ARGS("subst", xlat_func_subst, FR_TYPE_STRING, xlat_func_subst_args);