Browse Source

lang: Add anchor-debug feature flag for logging (#253)

Stanisław Drozd 4 years ago
parent
commit
d08ec0326e

+ 1 - 0
CHANGELOG.md

@@ -14,6 +14,7 @@ incremented for features.
 ## Features
 ## Features
 
 
 * client: Adds support for state instructions ([#248](https://github.com/project-serum/anchor/pull/248)).
 * client: Adds support for state instructions ([#248](https://github.com/project-serum/anchor/pull/248)).
+* lang: Add `anchor-debug` feature flag for logging ([#253](https://github.com/project-serum/anchor/pull/253)).
 * ts: Add support for u16 ([#255](https://github.com/project-serum/anchor/pull/255)).
 * ts: Add support for u16 ([#255](https://github.com/project-serum/anchor/pull/255)).
 
 
 ## Breaking
 ## Breaking

+ 11 - 0
lang/Cargo.toml

@@ -10,6 +10,17 @@ description = "Solana Sealevel eDSL"
 [features]
 [features]
 derive = []
 derive = []
 default = []
 default = []
+anchor-debug = [
+    "anchor-attribute-access-control/anchor-debug",
+    "anchor-attribute-account/anchor-debug",
+    "anchor-attribute-error/anchor-debug",
+    "anchor-attribute-event/anchor-debug",
+    "anchor-attribute-interface/anchor-debug",
+    "anchor-attribute-program/anchor-debug",
+    "anchor-attribute-program/anchor-debug",
+    "anchor-attribute-state/anchor-debug",
+    "anchor-derive-accounts/anchor-debug"
+]
 
 
 [dependencies]
 [dependencies]
 anchor-attribute-access-control = { path = "./attribute/access-control", version = "0.4.5" }
 anchor-attribute-access-control = { path = "./attribute/access-control", version = "0.4.5" }

+ 3 - 0
lang/attribute/access-control/Cargo.toml

@@ -10,6 +10,9 @@ edition = "2018"
 [lib]
 [lib]
 proc-macro = true
 proc-macro = true
 
 
+[features]
+anchor-debug = ["anchor-syn/anchor-debug"]
+
 [dependencies]
 [dependencies]
 proc-macro2 = "1.0"
 proc-macro2 = "1.0"
 quote = "1.0"
 quote = "1.0"

+ 3 - 0
lang/attribute/account/Cargo.toml

@@ -10,6 +10,9 @@ edition = "2018"
 [lib]
 [lib]
 proc-macro = true
 proc-macro = true
 
 
+[features]
+anchor-debug = ["anchor-syn/anchor-debug"]
+
 [dependencies]
 [dependencies]
 proc-macro2 = "1.0"
 proc-macro2 = "1.0"
 quote = "1.0"
 quote = "1.0"

+ 3 - 0
lang/attribute/error/Cargo.toml

@@ -10,6 +10,9 @@ edition = "2018"
 [lib]
 [lib]
 proc-macro = true
 proc-macro = true
 
 
+[features]
+anchor-debug = ["anchor-syn/anchor-debug"]
+
 [dependencies]
 [dependencies]
 proc-macro2 = "1.0"
 proc-macro2 = "1.0"
 quote = "1.0"
 quote = "1.0"

+ 3 - 0
lang/attribute/event/Cargo.toml

@@ -10,6 +10,9 @@ edition = "2018"
 [lib]
 [lib]
 proc-macro = true
 proc-macro = true
 
 
+[features]
+anchor-debug = ["anchor-syn/anchor-debug"]
+
 [dependencies]
 [dependencies]
 proc-macro2 = "1.0"
 proc-macro2 = "1.0"
 quote = "1.0"
 quote = "1.0"

+ 3 - 0
lang/attribute/interface/Cargo.toml

@@ -10,6 +10,9 @@ edition = "2018"
 [lib]
 [lib]
 proc-macro = true
 proc-macro = true
 
 
+[features]
+anchor-debug = ["anchor-syn/anchor-debug"]
+
 [dependencies]
 [dependencies]
 proc-macro2 = "1.0"
 proc-macro2 = "1.0"
 quote = "1.0"
 quote = "1.0"

+ 3 - 0
lang/attribute/program/Cargo.toml

@@ -10,6 +10,9 @@ edition = "2018"
 [lib]
 [lib]
 proc-macro = true
 proc-macro = true
 
 
+[features]
+anchor-debug = ["anchor-syn/anchor-debug"]
+
 [dependencies]
 [dependencies]
 proc-macro2 = "1.0"
 proc-macro2 = "1.0"
 quote = "1.0"
 quote = "1.0"

+ 3 - 0
lang/attribute/state/Cargo.toml

@@ -10,6 +10,9 @@ edition = "2018"
 [lib]
 [lib]
 proc-macro = true
 proc-macro = true
 
 
+[features]
+anchor-debug = ["anchor-syn/anchor-debug"]
+
 [dependencies]
 [dependencies]
 proc-macro2 = "1.0"
 proc-macro2 = "1.0"
 quote = "1.0"
 quote = "1.0"

+ 4 - 0
lang/derive/accounts/Cargo.toml

@@ -10,6 +10,10 @@ edition = "2018"
 [lib]
 [lib]
 proc-macro = true
 proc-macro = true
 
 
+[features]
+default = []
+anchor-debug = ["anchor-syn/anchor-debug"]
+
 [dependencies]
 [dependencies]
 proc-macro2 = "1.0"
 proc-macro2 = "1.0"
 quote = "1.0"
 quote = "1.0"

+ 1 - 0
lang/syn/Cargo.toml

@@ -11,6 +11,7 @@ edition = "2018"
 idl = []
 idl = []
 hash = []
 hash = []
 default = []
 default = []
+anchor-debug = []
 
 
 [dependencies]
 [dependencies]
 proc-macro2 = "1.0"
 proc-macro2 = "1.0"

+ 3 - 3
lang/syn/src/codegen/accounts.rs

@@ -21,7 +21,7 @@ pub fn generate(accs: AccountsStruct) -> proc_macro2::TokenStream {
                     let name = &s.ident;
                     let name = &s.ident;
                     let ty = &s.raw_field.ty;
                     let ty = &s.raw_field.ty;
                     quote! {
                     quote! {
-                        #[cfg(feature = "debug-log")]
+                        #[cfg(feature = "anchor-debug")]
                         ::solana_program::log::sol_log(stringify!(#name));
                         ::solana_program::log::sol_log(stringify!(#name));
                         let #name: #ty = anchor_lang::Accounts::try_accounts(program_id, accounts)?;
                         let #name: #ty = anchor_lang::Accounts::try_accounts(program_id, accounts)?;
                     }
                     }
@@ -40,12 +40,12 @@ pub fn generate(accs: AccountsStruct) -> proc_macro2::TokenStream {
                         let name = &f.typed_ident();
                         let name = &f.typed_ident();
                         match f.is_init {
                         match f.is_init {
                             false => quote! {
                             false => quote! {
-                                #[cfg(feature = "debug-log")]
+                                #[cfg(feature = "anchor-debug")]
                                 ::solana_program::log::sol_log(stringify!(#name));
                                 ::solana_program::log::sol_log(stringify!(#name));
                                 let #name = anchor_lang::Accounts::try_accounts(program_id, accounts)?;
                                 let #name = anchor_lang::Accounts::try_accounts(program_id, accounts)?;
                             },
                             },
                             true => quote! {
                             true => quote! {
-                                #[cfg(feature = "debug-log")]
+                                #[cfg(feature = "anchor-debug")]
                                 ::solana_program::log::sol_log(stringify!(#name));
                                 ::solana_program::log::sol_log(stringify!(#name));
                                 let #name = anchor_lang::AccountsInit::try_accounts_init(program_id, accounts)?;
                                 let #name = anchor_lang::AccountsInit::try_accounts_init(program_id, accounts)?;
                             },
                             },

+ 4 - 0
lang/syn/src/codegen/program.rs

@@ -67,6 +67,10 @@ pub fn generate(program: Program) -> proc_macro2::TokenStream {
         /// program, where execution begins.
         /// program, where execution begins.
         #[cfg(not(feature = "no-entrypoint"))]
         #[cfg(not(feature = "no-entrypoint"))]
         fn entry(program_id: &Pubkey, accounts: &[AccountInfo], ix_data: &[u8]) -> ProgramResult {
         fn entry(program_id: &Pubkey, accounts: &[AccountInfo], ix_data: &[u8]) -> ProgramResult {
+            #[cfg(feature = "anchor-debug")]
+            {
+                msg!("anchor-debug is active");
+            }
             if ix_data.len() < 8 {
             if ix_data.len() < 8 {
                 return Err(ProgramError::Custom(99));
                 return Err(ProgramError::Custom(99));
             }
             }