45

Sometimes it would be really helpful to be able to see string representations of variable values in the Geth console, like of argument values in functions. Can I somehow print them to the Geth console?

eth
  • 85,679
  • 53
  • 285
  • 406
TMOTTM
  • 1,953
  • 2
  • 14
  • 23

3 Answers3

26

You are most likely looking for Events. Not only do they help with debugging, but they are useful in normal production code.

Events are declared like functions, like so:

event VoteCast(address voter, uint votes, bool inFavor);

Then somewhere (for example, in a vote counting function):

function vote(bool inFavor) {
    var votes = shares[msg.sender];
    // ...
    emit VoteCast(msg.sender, votes, inFavor);
}

In javascript, a contract object has an event method, which can be used to read events when they happen. In fact, it can read events that happened in the past.

var voteCast = someContract.voteCast();
voteCast.watch(function(err, result) {/* some callback */});
// Alternately, to get the events all at once.
voteCast.get(function(err, result) /* some other callback* /)

Events have a number of quirks, which are too many to get into detail here. Nonetheless, they're an integral part of high-level dapp work.

jlo
  • 263
  • 3
  • 10
Matthew Schmidt
  • 7,290
  • 1
  • 24
  • 35
16

EDIT: logX is deprecated and doesn't appear in Solidity 0.8+ documentation. The bellow example is only working if using Solidity up to version 0.7.6 and should now be replaced by Events or Solidity libraries enabling the use of console.log() inside your contract code.

Since my first answer, many projects improved the debug experience:


Deprecated answer

Print doesn't exist in Solidity. Use the logX statement as indicated in the manual instead.

Nicolas Massart
  • 6,783
  • 2
  • 29
  • 63
  • 1
    The log statements are really low level. One is better off using specific events. – Matthew Schmidt Jul 17 '16 at 16:10
  • 5
    Agree they're low-level but can be useful especially for temporary debugging to quickly see a value in IDEs that support log statements and pretty-print their parameters. – eth Nov 09 '16 at 11:06
2

Since you mentioned geth console, you can try console.log() for debugging purposes:

console.log ("Your debug message" + debug.object);

This is possible because geth supports web3.js

CᴴᴀZ
  • 161
  • 6
  • 7
    this is js way , not solidity – maxisacoder Sep 10 '18 at 13:02
  • @MaxLXJ print is not supported by Solidity as it supports Events-driven paradigm, but since it leverages JavaScript syntax it is quite handy to use console.log for debug purposes. But this is not recommended for Production/Live usage as Solidity written contract will be distributed across nodes, and using Events will be ideal. – CᴴᴀZ Sep 20 '18 at 11:06
  • 1
    It's true that is not solidity, but probably it's what most people want: an easy way to print something in the console. This works if you write the console.log in the console and not in the solidity code. – Marco Altieri Aug 07 '21 at 10:03
  • 1
    @MarcoAltieri could you provide an example of using console.log within solidity? This seems like a major miss in the language implementation – Geoff Langenderfer May 30 '22 at 17:55
  • @CᴴᴀZ 's link gives an example to print an event to the console – Geoff Langenderfer May 30 '22 at 17:57
  • @GeoffLangenderfer Yes, and they are using console.log(result); to print the result to console. – CᴴᴀZ Jun 01 '22 at 09:35