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

[logging] add lint-logs.sh to check for newline termination. #12891

Merged
merged 2 commits into from Apr 8, 2018

Conversation

jnewbery
Copy link
Contributor

@jnewbery jnewbery commented Apr 5, 2018

Check that all calls to LogPrintf() are terminated by a newline,
except those that are explicitly marked as 'continued' logs.

@jnewbery
Copy link
Contributor Author

jnewbery commented Apr 5, 2018

Suggested by @MarcoFalke here: #12887 (comment)

linter hints suggested by @laanwj here: #12887 (comment)

@@ -63,7 +63,7 @@ class CBitcoinLevelDBLogger : public leveldb::Logger {

assert(p <= limit);
base[std::min(bufsize - 1, (int)(p - base))] = '\0';
LogPrintf("leveldb: %s", base);
LogPrintf("leveldb: %s", base); /* Continued */
Copy link
Member

Choose a reason for hiding this comment

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

Since you are changing those lines anyway, why not use LogPrintfContinuation or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My suggestion here: #12887 (comment) has inverted semantics. This Continued here comment indicates that there is a continuation log to follow. LogPrintfContinuation() would indicate that the log continues a previous log.

I think LogPrintfContinuation() could be useful, but it's a rebase-the-world PR since it removes the \n from every call to LogPrintf()

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please don't change every single log message. This solution is fine.

@maflcko
Copy link
Member

maflcko commented Apr 5, 2018

utACK 28682f3354a89f49a402735f94b7f79f34966aab

@laanwj
Copy link
Member

laanwj commented Apr 5, 2018

utACK 28682f3

@practicalswift
Copy link
Contributor

practicalswift commented Apr 5, 2018

Very strong concept ACK. Thanks for making this fix persistent by adding a lint script.

Please apply this patch to the lint script to make it work as expected:

--- lint-logs.sh	2018-04-05 22:43:14.119412169 +0200
+++ lint-logs.sh.fixed	2018-04-05 22:42:03.787354149 +0200
@@ -13,10 +13,10 @@
 # ignored


-UNTERMINATED_LOGS=$(git grep "LogPrintf(" -- *cpp | \
-	grep -v "\\\n\"" | \
-	grep -v "/\* Continued \*/" | \
-	grep -v "LogPrintf\(\)")
+UNTERMINATED_LOGS=$(git grep "LogPrintf(" -- "*.cpp" | \
+    grep -v '\\n"' | \
+    grep -v "/\* Continued \*/" | \
+    grep -v "LogPrintf()")
 if [[ ${UNTERMINATED_LOGS} != "" ]]; then
     echo "All calls to LogPrintf() should be terminated with \\n"
     echo

The bugs:

  • The *cpp in git grep "LogPrintf(" -- *cpp will be expanded by the shell and match only cpp files in the current directory.
  • grep -v "LogPrintf\(\)" is equivalent to grep -v "LogPrintf" due to how basic regular expressions are handled by grep. From the documentation: "In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions ?, +, {, |, (, and )."

ACK modulo fixing the lint script bugs :-)

@jnewbery
Copy link
Contributor Author

jnewbery commented Apr 6, 2018

Thanks for the review and bugfixes @practicalswift ! Your fixed version caught a non-conforming log, which I've fixed in a new commit.

@maflcko
Copy link
Member

maflcko commented Apr 6, 2018

utACK 621981530b57ac983b7bf35474c0796fbf5d5ed7

1 similar comment
@practicalswift
Copy link
Contributor

utACK 621981530b57ac983b7bf35474c0796fbf5d5ed7

@@ -3076,7 +3076,7 @@ bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::ve
wtxNew.fTimeReceivedIsTxTime = true;
wtxNew.fFromMe = true;

LogPrintf("CommitTransaction:\n%s", wtxNew.tx->ToString());
LogPrintf("CommitTransaction:\n%s\n", wtxNew.tx->ToString());
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately this change is wrong, as tx->ToString() adds a final newline: https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.cpp#L99

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, well spotted. Now fixed.

Incidentally, I hate this log. As far as I'm aware it's the only one with newlines embedded and it confuses my log parser!

Most logs should terminated with a '\n'. Some logs
are built up over multiple calls to logPrintf(), so
do not need a newline terminater. Comment all of
these 'continued' logs as a linter hing.
Check that all calls to LogPrintf() are terminated by a newline,
except those that are explicitly marked as 'continued' logs.
@practicalswift
Copy link
Contributor

utACK d207207

@laanwj
Copy link
Member

laanwj commented Apr 8, 2018

utACK d207207

@laanwj laanwj merged commit d207207 into bitcoin:master Apr 8, 2018
laanwj added a commit that referenced this pull request Apr 8, 2018
…tion.

d207207 [logging] add lint-logs.sh to check for newline termination. (John Newbery)
5c21e6c [logging] Comment all continuing logs. (John Newbery)

Pull request description:

  Check that all calls to LogPrintf() are terminated by a newline,
  except those that are explicitly marked as 'continued' logs.

Tree-SHA512: fe5162b2b2df1e8a4c807da87584fa9af97a6b8377e4090fe0caa136d90bf29a487a123cde94569bdce7101fee3478196d99aa13f1212e24bfe5f41c773604fc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2020
…termination.

d207207 [logging] add lint-logs.sh to check for newline termination. (John Newbery)
5c21e6c [logging] Comment all continuing logs. (John Newbery)

Pull request description:

  Check that all calls to LogPrintf() are terminated by a newline,
  except those that are explicitly marked as 'continued' logs.

Tree-SHA512: fe5162b2b2df1e8a4c807da87584fa9af97a6b8377e4090fe0caa136d90bf29a487a123cde94569bdce7101fee3478196d99aa13f1212e24bfe5f41c773604fc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2020
…termination.

d207207 [logging] add lint-logs.sh to check for newline termination. (John Newbery)
5c21e6c [logging] Comment all continuing logs. (John Newbery)

Pull request description:

  Check that all calls to LogPrintf() are terminated by a newline,
  except those that are explicitly marked as 'continued' logs.

Tree-SHA512: fe5162b2b2df1e8a4c807da87584fa9af97a6b8377e4090fe0caa136d90bf29a487a123cde94569bdce7101fee3478196d99aa13f1212e24bfe5f41c773604fc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 10, 2020
…termination.

d207207 [logging] add lint-logs.sh to check for newline termination. (John Newbery)
5c21e6c [logging] Comment all continuing logs. (John Newbery)

Pull request description:

  Check that all calls to LogPrintf() are terminated by a newline,
  except those that are explicitly marked as 'continued' logs.

Tree-SHA512: fe5162b2b2df1e8a4c807da87584fa9af97a6b8377e4090fe0caa136d90bf29a487a123cde94569bdce7101fee3478196d99aa13f1212e24bfe5f41c773604fc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2020
…termination.

d207207 [logging] add lint-logs.sh to check for newline termination. (John Newbery)
5c21e6c [logging] Comment all continuing logs. (John Newbery)

Pull request description:

  Check that all calls to LogPrintf() are terminated by a newline,
  except those that are explicitly marked as 'continued' logs.

Tree-SHA512: fe5162b2b2df1e8a4c807da87584fa9af97a6b8377e4090fe0caa136d90bf29a487a123cde94569bdce7101fee3478196d99aa13f1212e24bfe5f41c773604fc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2020
…termination.

d207207 [logging] add lint-logs.sh to check for newline termination. (John Newbery)
5c21e6c [logging] Comment all continuing logs. (John Newbery)

Pull request description:

  Check that all calls to LogPrintf() are terminated by a newline,
  except those that are explicitly marked as 'continued' logs.

Tree-SHA512: fe5162b2b2df1e8a4c807da87584fa9af97a6b8377e4090fe0caa136d90bf29a487a123cde94569bdce7101fee3478196d99aa13f1212e24bfe5f41c773604fc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 11, 2020
…termination.

d207207 [logging] add lint-logs.sh to check for newline termination. (John Newbery)
5c21e6c [logging] Comment all continuing logs. (John Newbery)

Pull request description:

  Check that all calls to LogPrintf() are terminated by a newline,
  except those that are explicitly marked as 'continued' logs.

Tree-SHA512: fe5162b2b2df1e8a4c807da87584fa9af97a6b8377e4090fe0caa136d90bf29a487a123cde94569bdce7101fee3478196d99aa13f1212e24bfe5f41c773604fc
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 12, 2020
…termination.

d207207 [logging] add lint-logs.sh to check for newline termination. (John Newbery)
5c21e6c [logging] Comment all continuing logs. (John Newbery)

Pull request description:

  Check that all calls to LogPrintf() are terminated by a newline,
  except those that are explicitly marked as 'continued' logs.

Tree-SHA512: fe5162b2b2df1e8a4c807da87584fa9af97a6b8377e4090fe0caa136d90bf29a487a123cde94569bdce7101fee3478196d99aa13f1212e24bfe5f41c773604fc
@str4d str4d mentioned this pull request Apr 12, 2021
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants