Skip to content

Instantly share code, notes, and snippets.

@maurelian
Last active February 25, 2022 18:44
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save maurelian/4f7402ba5641b96eca56f703fcae37c2 to your computer and use it in GitHub Desktop.
Save maurelian/4f7402ba5641b96eca56f703fcae37c2 to your computer and use it in GitHub Desktop.
A JS style console.log() function for solidity.
pragma solidity ^0.4.10;
// Update: Just use HardHat's: https://github.com/nomiclabs/hardhat/blob/master/packages/hardhat-core/console.sol
// Enables event logging of the format `console.log('descriptive string', variable)`,
// without having to worry about the variable type (as long as an event has been declared for that type in the
// Console contract.
contract Console {
event LogUint(string, uint);
function log(string s , uint x) {
LogUint(s, x);
}
event LogInt(string, int);
function log(string s , int x) {
LogInt(s, x);
}
event LogBytes(string, bytes);
function log(string s , bytes x) {
LogBytes(s, x);
}
event LogBytes32(string, bytes32);
function log(string s , bytes32 x) {
LogBytes32(s, x);
}
event LogAddress(string, address);
function log(string s , address x) {
LogAddress(s, x);
}
event LogBool(string, bool);
function log(string s , bool x) {
LogBool(s, x);
}
}
contract NeedsDebugging is Console {
function InspectMePlease(){
log("name", uint(1));
}
}
@fasidOnGit
Copy link

Just great.!

@mlibre
Copy link

mlibre commented Nov 5, 2021

2021 version!

contract Console {
    event LogUint(string, uint256);

    function log(string memory s, uint256 x) public {
        emit LogUint(s, x);
    }

    event LogInt(string, int256);

    function log(string memory s, int256 x) public {
        emit LogInt(s, x);
    }

    event LogBytes(string, bytes);

    function log(string memory s, bytes memory x) public {
        emit LogBytes(s, x);
    }

    event LogBytes32(string, bytes32);

    function log(string memory s, bytes32 x) public {
        emit LogBytes32(s, x);
    }

    event LogAddress(string, address);

    function log(string memory s, address x) public {
        emit LogAddress(s, x);
    }

    event LogBool(string, bool);

    function log(string memory s, bool x) public {
        emit LogBool(s, x);
    }

    event LogString(string);

    function log(string memory s) public {
        emit LogString(s);
    }
}

@maurelian
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment