couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject [18/29] twig commit: updated refs/heads/import to 2d56280
Date Mon, 03 Feb 2014 22:12:32 GMT
Truncate large messages

Behavior is controlled by two application environment settings:

  max_term_size - raw data size below which we format normally (8192)
  max_message_size - approx. max size of truncated string (16000)


Project: http://git-wip-us.apache.org/repos/asf/couchdb-twig/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-twig/commit/328e707d
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-twig/tree/328e707d
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-twig/diff/328e707d

Branch: refs/heads/import
Commit: 328e707da326b7e65afe4f4c38a3758c7c43b80d
Parents: eaa15f1
Author: Adam Kocoloski <adam@cloudant.com>
Authored: Wed Mar 9 12:53:12 2011 -0500
Committer: Adam Kocoloski <adam@cloudant.com>
Committed: Wed Mar 9 13:03:06 2011 -0500

----------------------------------------------------------------------
 src/twig.erl               |  2 +-
 src/twig_event_handler.erl | 43 ++++++++++++++++-------------------------
 src/twig_util.erl          | 20 ++++++++++++++++++-
 3 files changed, 37 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-twig/blob/328e707d/src/twig.erl
----------------------------------------------------------------------
diff --git a/src/twig.erl b/src/twig.erl
index 191c25b..b97f51b 100644
--- a/src/twig.erl
+++ b/src/twig.erl
@@ -46,7 +46,7 @@ format(Level, Format, Data) ->
     %% TODO truncate large messages
     #twig{
         level = Level,
-        msg = iolist_to_binary(io_lib:format(Format, Data)),
+        msg = iolist_to_binary(twig_util:format(Format, Data)),
         msgid = erlang:get(nonce),
         pid = self()
     }.

http://git-wip-us.apache.org/repos/asf/couchdb-twig/blob/328e707d/src/twig_event_handler.erl
----------------------------------------------------------------------
diff --git a/src/twig_event_handler.erl b/src/twig_event_handler.erl
index 2dbab71..b548339 100644
--- a/src/twig_event_handler.erl
+++ b/src/twig_event_handler.erl
@@ -17,6 +17,8 @@
 -export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2,
         code_change/3]).
 
+-import(twig_util, [get_env/2]).
+
 -record(state, {
     socket,
     host,
@@ -88,33 +90,15 @@ terminate(_Reason, State) ->
 code_change(_OldVsn, State, _Extra) ->
     {ok, State}.
 
-get_env(Key, Default) ->
-    case application:get_env(twig, Key) of
-        {ok, Value} ->
-            Value;
-        undefined ->
-            Default
-    end.
-
 write(Level, undefined, Msg, Pid, State) ->
     write(Level, "--------", Msg, Pid, State);
 write(Level, MsgId, Msg, Pid, State) when is_list(Msg); is_binary(Msg) ->
     #state{facility=Facil, appid=App, hostname=Hostname, host=Host, port=Port,
         socket=Socket} = State,
-     Pre = io_lib:format("<~B>~B ~s ~s ~s ~p ~s - ", [Facil bor Level,
+    Pre = io_lib:format("<~B>~B ~s ~s ~s ~p ~s - ", [Facil bor Level,
         ?SYSLOG_VERSION, twig_util:iso8601_timestamp(), Hostname, App, Pid,
         MsgId]),
-    %% TODO truncate large messages
-     send(Socket, Host, Port, [Pre, Msg, $\n]);
-write(Level, MsgId, {Format0, Args0}, Pid, State) ->
-    #state{facility=Facil, appid=App, hostname=Hostname, host=Host, port=Port,
-        socket=Socket} = State,
-    Format = "<~B>~B ~s ~s ~s ~p ~s - " ++ Format0 ++ "\n",
-    Args = [Facil bor Level, ?SYSLOG_VERSION, twig_util:iso8601_timestamp(),
-        Hostname, App, Pid, MsgId | Args0],
-    %% TODO truncate large messages
-    Packet = io_lib:format(Format, Args),
-    send(Socket, Host, Port, Packet).
+    send(Socket, Host, Port, [Pre, Msg, $\n]).
 
 send(_, undefined, _, Packet) ->
     io:put_chars(Packet);
@@ -122,7 +106,14 @@ send(Socket, Host, Port, Packet) ->
     gen_udp:send(Socket, Host, Port, Packet).
 
 message(crash_report, Report) ->
-    {crash_report, proc_lib:format(Report)};
+    Msg = case erts_debug:flat_size(Report) > get_env(max_term_size, 8192) of
+        true ->
+            MaxString = get_env(max_message_size, 16000),
+            ["*Truncated* - ", trunc_io:print(Report, MaxString)];
+        false ->
+            proc_lib:format(Report)
+    end,
+    {crash_report, Msg};
 message(supervisor_report, Report) ->
     Name = get_value(supervisor, Report),
     Error = get_value(errorContext, Report),
@@ -136,18 +127,18 @@ message(supervisor_report, Report) ->
         {M,F,_} ->
             ok
     end,
-    {supervisor_report, {"~p ~p (~p) child: ~p [~p] ~p:~p",
-            [Name, Error, Reason, ChildName, ChildPid, M, F]}};
+    {supervisor_report, twig_util:format("~p ~p (~p) child: ~p [~p] ~p:~p",
+            [Name, Error, Reason, ChildName, ChildPid, M, F])};
 message(Type, Report) when Type == std_error;
                            Type == std_info;
                            Type == std_warning;
                            Type == progress_report;
                            Type == progress ->
-    {Type, {"~2048.0p", [Report]}};
+    {Type, twig_util:format("~2048.0p", [Report])};
 message(Format, Args) when is_list(Format) ->
-    {msg, {Format, Args}};
+    {msg, twig_util:format(Format, Args)};
 message(Format, Args) ->
-    {unknown, {"~2048.0p ~2048.0p", [Format, Args]}}.
+    {unknown, twig_util:format("~2048.0p ~2048.0p", [Format, Args])}.
 
 otp_event_level(error, _) ->                        ?LEVEL_ERR;
 otp_event_level(warning_msg, _) ->                  ?LEVEL_WARN;

http://git-wip-us.apache.org/repos/asf/couchdb-twig/blob/328e707d/src/twig_util.erl
----------------------------------------------------------------------
diff --git a/src/twig_util.erl b/src/twig_util.erl
index 19ec802..943ef1a 100644
--- a/src/twig_util.erl
+++ b/src/twig_util.erl
@@ -12,7 +12,7 @@
 
 -module(twig_util).
 
--export([level/1, facility/1, iso8601_timestamp/0]).
+-export([format/2, get_env/2, level/1, facility/1, iso8601_timestamp/0]).
 
 level(debug) ->     7;
 level(info) ->      6;
@@ -56,3 +56,21 @@ iso8601_timestamp() ->
     {{Year,Month,Date},{Hour,Minute,Second}} = calendar:now_to_datetime(Now),
     Format = "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0B.~6.10.0BZ",
     io_lib:format(Format, [Year, Month, Date, Hour, Minute, Second, Micro]).
+
+format(Format, Data) ->
+    MaxTermSize = get_env(max_term_size, 8192),
+    case erts_debug:flat_size(Data) > MaxTermSize of
+        true ->
+            MaxString = get_env(max_message_size, 16000),
+            ["*Truncated* ", Format, " - ", trunc_io:print(Data, MaxString)];
+        false ->
+            io_lib:format(Format, Data)
+    end.
+
+get_env(Key, Default) ->
+    case application:get_env(twig, Key) of
+        {ok, Value} ->
+            Value;
+        undefined ->
+            Default
+    end.


Mime
View raw message