@@ -21,7 +21,7 @@ local TABLE_TOSTRING_SEP_LEN = string.len(TABLE_TOSTRING_SEP)
2121
2222-- Final function called in format_table() to format the resulting list of
2323-- string describing the table.
24- local function _table_tostring_format_result (tbl , result , indentLevel , printTableRefs )
24+ local function _table_tostring_format_result (tbl , result , indentLevel , printTableRefs , isLogLine )
2525 local dispOnMultLines = false
2626
2727 -- set dispOnMultLines to true if the maximum LINE_LENGTH would be exceeded with the values
@@ -46,7 +46,7 @@ local function _table_tostring_format_result(tbl, result, indentLevel, printTabl
4646 end
4747
4848 -- now reformat the result table (currently holding element strings)
49- if dispOnMultLines then
49+ if dispOnMultLines and not isLogLine then
5050 local indentString = string.rep (" " , indentLevel - 1 )
5151 result = {
5252 " {\n " ,
@@ -77,7 +77,7 @@ function Formatter.mt:initialize(printTableRefs)
7777 self .recursionTable = {}
7878end
7979
80- function Formatter .mt :format_table (tbl , indentLevel )
80+ function Formatter .mt :format_table (tbl , indentLevel , isLogLine )
8181 indentLevel = indentLevel or 1
8282 self .recursionTable [tbl ] = true
8383
@@ -133,16 +133,16 @@ function Formatter.mt:format_table(tbl, indentLevel)
133133 count = count + 1
134134 result [count ] = entry
135135 end
136- return _table_tostring_format_result (tbl , result , indentLevel , self .printTableRefs )
136+ return _table_tostring_format_result (tbl , result , indentLevel , self .printTableRefs , isLogLine )
137137 end
138138end
139139
140- function Formatter .mt :format (v , indentLevel )
140+ function Formatter .mt :format (v , indentLevel , isLogLine )
141141 local type_v = type (v )
142142 if " string" == type_v then
143143 return string.format (" %q" , v )
144144 elseif " table" == type_v then
145- return self :format_table (v , indentLevel )
145+ return self :format_table (v , indentLevel , isLogLine )
146146 elseif " number" == type_v then
147147 -- eliminate differences in formatting between various Lua versions
148148 if v ~= v then
@@ -169,18 +169,24 @@ end
169169--
170170-- * string are enclosed with " by default, or with ' if string contains a "
171171-- * tables are expanded to show their full content, with indentation in case of nested tables
172- function pp .tostring (value )
172+ function pp .tostring (value , is_logline )
173173 local formatter = Formatter :new (pp .TABLE_REF_IN_ERROR_MSG )
174- local result = formatter :format (value )
174+ local result = formatter :format (value , nil , is_logline )
175175 if formatter .recursionDetected and not pp .TABLE_REF_IN_ERROR_MSG then
176176 -- some table contain recursive references,
177177 -- so we must recompute the value by including all table references
178178 -- else the result looks like crap
179- return Formatter :new (true ):format (value )
179+ return Formatter :new (true ):format (value , nil , is_logline )
180180 end
181181 return result
182182end
183183
184+ -- This function helps with displaying `value` of any type without line breaks ('\n')
185+ -- for logging. It is a simple wrapper over the tostring() function.
186+ function pp .tostringlog (value )
187+ return pp .tostring (value , true )
188+ end
189+
184190local function has_new_line (s )
185191 return (string.find (s , ' \n ' , 1 , true ) ~= nil )
186192end
0 commit comments